Chapter 14. Creating and executing queries

 

In this chapter

  • The basic query APIs
  • Creating and preparing queries
  • Optimizing query execution

If you’ve been using handwritten SQL for a number of years, you may be concerned that ORM will take away some of the expressiveness and flexibility you’re used to. This isn’t the case with Hibernate and Java Persistence.

With Hibernate’s and Java Persistence’s powerful query facilities, you can express almost everything you commonly (or even uncommonly) need to express in SQL, but in object-oriented terms—using classes and properties of classes. Moreover, you can always fall back to SQL strings and let Hibernate do the heavy lifting of handling the query result. For additional SQL resources, consult our reference section.

Major new features in JPA 2
  • A type-safe criteria API for the programmatic creation of queries is now available.
  • You can now declare up front the type of a query result with the new Typed-Query interface.
  • You can programmatically save a Query (JPQL, criteria, or native SQL) for later use as a named query.
  • In addition to being able to set query parameters, hints, maximum results, and flush and lock modes, JPA 2 extends the Query API with various getter methods for obtaining the current settings.
  • JPA now standardizes several query hints (timeout, cache usage).

14.1. Creating queries

 
 
 
 

14.2. Preparing queries

 
 
 
 

14.3. Executing queries

 
 
 

14.4. Naming and externalizing queries

 
 
 
 

Path: /model/src/main/resources/querying/ExternalizedQueries.xml

<entity-mappings
   version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
            http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
<enter/>
    <named-query name="findItems">
        <query><![CDATA[
            select i from Item i
        ]]></query>
    </named-query>
<enter/>
</entity-mappings>
 
 

Path: /model/src/main/resources/querying/ExternalizedQueries.xml

<named-native-query name="findItemsSQL"
                    result-class="org.jpwh.model.querying.Item">
    <query>select * from ITEM</query>
</named-native-query>
 
 
 

Path: /model/src/main/resources/querying/ExternalizedQueries.hbm.xml

<?xml version="1.0"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/orm/hbm">
<enter/>
    <query name="findItemsOrderByAuctionEndHibernate">
        select i from Item i order by i.auctionEnd asc
    </query>
<enter/>
    <sql-query name="findItemsSQLHibernate">
        <return class="org.jpwh.model.querying.Item"/>
        select * from ITEM order by NAME asc
    </sql-query>
<enter/>
</hibernate-mapping>
 
 
 
 

Path: /model/src/main/java/org/jpwh/model/querying/package-info.java

@org.hibernate.annotations.NamedQueries({
    @org.hibernate.annotations.NamedQuery(
        name = "findItemsOrderByName",
        query = "select i from Item i order by i.name asc"
    )
})
<enter/>
package org.jpwh.model.querying;
 

14.5. Query hints

 
 

14.6. Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest