CHAPTER 14 Querying with HQL and JPA QL (Space web hosting)

CHAPTER 14 Querying with HQL and JPA QL Most of these HQL functions translate into a counterpart in SQL you ve probably used before. This translation table is customizable and extendable with an org.hibernate.Dialect. Check the source code of the dialect you re using for your database; you ll probably find many other SQL functions already registered there for immediate use in HQL. Keep in mind that every function that isn t included in the org.hibernate.Dialect superclass may not be portable to other database management systems! Another recent addition to the Hibernate API is the addSqlFunction() method on the Hibernate Configuration API: Configuration cfg = new Configuration(); cfg.addSqlFunction( “lpad”, new StandardSQLFunction(”lpad”, Hibernate.STRING) ); … cfg.buildSessionFactory(); This operation adds the SQL function lpad to HQL. See the Javadoc of Standard- SQLFunction and its subclasses for more information. HQL even tries to be smart when you call a function that wasn t registered for your SQL dialect: Any function that is called in the WHERE clause of an HQL statement, and that isn t known to Hibernate, is passed directly to the database, as an SQL function call. This works great if you don t care about database portability, but it requires that you keep your eyes open for nonportable functions if you do care. Finally, before we move on to the SELECT clause in HQL and JPA QL, let s see how results can be ordered. Ordering query results All query languages provide some mechanism for ordering query results. HQL and JPA QL provide an ORDER BY clause, similar to SQL. This query returns all users, ordered by username: from User u order by u.username You specify ascending and descending order using asc or desc: from User u order by u.username desc You may order by multiple properties: from User u order by u.lastname asc, u.firstname asc You now know how to write a FROM, WHERE, and ORDER BY clause. You know how to select the entities you want to retrieve instances of and the necessary expressions
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Leave a Reply