CHAPTER 14 Querying with HQL and JPA QL (Web hosting top)

CHAPTER 14 Querying with HQL and JPA QL 1 Create the query, with any arbitrary restriction or projection of data that you want to retrieve. 2 Bind runtime arguments to query parameters; the query can be reused with changing settings. 3 Execute the prepared query against the database and retrieval of data. You can control how the query is executed and how data should be retrieved into memory (all at once or piecemeal, for example). Hibernate and Java Persistence offer query interfaces and methods on these interfaces to prepare and execute arbitrary data retrieval operations. 14.1.1 Preparing a query The org.hibernate.Query and org.hibernate.Criteria interfaces both define several methods for controlling execution of a query. In addition, Query provides methods for binding concrete values to query parameters. To execute a query in your application, you need to obtain an instance of one of these interfaces, using the Session. Java Persistence specifies the javax.persistence.Query interface. The standardized interface isn t as rich as the native Hibernate API, but offers all necessary methods to execute a query in different ways and to bind arguments to query parameters. Unfortunately, the useful Hibernate Criteria API has no equivalent in Java Persistence, although it s highly likely that a similar query interface will be added in a future version of the standard. Creating a query object To create a new Hibernate Query instance, call either createQuery() or create- SQLQuery() on a Session. The createQuery() method prepares an HQL query: Query hqlQuery = session.createQuery(”from User”); createSQLQuery() is used to create an SQL query using the native syntax of the underlying database: Query sqlQuery = session.createSQLQuery( “select {user.*} from USERS {user}” ).addEntity(”user”, User.class); In both cases, Hibernate returns a newly instantiated Query object that may be used to specify exactly how a particular query should be executed and to allow execution of the query. So far, no SQL has been sent to the database.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Leave a Reply