Web hosting company - Basic HQL and JPA QL queries 14.2 Basic

Basic HQL and JPA QL queries 14.2 Basic HQL and JPA QL queries Let s start with some simple queries to get familiar with the HQL syntax and semantics. We apply selection to name the data source, restriction to match records to the criteria, and projection to select the data you want returned from a query. TRY IT Testing Hibernate queries The Hibernate Tools for the Eclipse IDE support a Hibernate Console view. You can test your queries in the console window, and see the generated SQL and the result immediately. You ll also learn JPA QL in this section, because it s a subset of the functionality of HQL we ll mention the differences when necessary. When we talk about queries in this section, we usually mean SELECT statements, operations that retrieve data from the database. HQL also supports UPDATE, DELETE, and even INSERT..SELECT statements, as we discussed in chapter 12, section 12.2.1, Bulk statements with HQL and JPA QL. JPA QL includes UPDATE and DELETE. We won t repeat these bulk operations here and will focus on SELECT statements. However, keep in mind that some differences between HQL and JPA QL may also apply to bulk operations for example, whether a particular function is portable. SELECT statements in HQL work even without a SELECT clause; only FROM is required. This isn t the case in JPA QL, where the SELECT clause isn t optional. This isn t a big difference in practice; almost all queries require a SELECT clause, whether you write JPA QL or HQL. However, we start our exploration of queries with the FROM clause, because in our experience it s easier to understand. Keep in mind that to translate these queries to JPA QL, you must theoretically add a SELECT clause to complete the statement, but Hibernate lets you execute the query anyway if you forget it (assuming SELECT *). 14.2.1 Selection The simplest query in HQL is a selection (note that we don t mean SELECT clause or statement here, but from where data is selected) of a single persistent class: from Item This query generates the following SQL: select i.ITEM_ID, i.NAME, i.DESCRIPTION, … from ITEM i Using aliases Usually, when you select a class to query from using HQL or JPA QL, you need to assign an alias to the queried class to use as a reference in other parts of the query:
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Leave a Reply