PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/hibernate-4.2.0/documentation/devguide/en-US/html/ch12.html

https://gitlab.com/essere.lab.public/qualitas.class-corpus
HTML | 510 lines | 410 code | 100 blank | 0 comment | 0 complexity | bf8828fde6a0c4ed7297cdad8b674a1c MD5 | raw file
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html
  3. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 12. Criteria</title><link rel="stylesheet" type="text/css" href="css/hibernate.css"/><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"/><link rel="home" href="index.html" title="Hibernate Developer Guide"/><link rel="up" href="index.html" title="Hibernate Developer Guide"/><link rel="prev" href="ch11.html" title="Chapter 11. HQL and JPQL"/><link rel="next" href="ch13.html" title="Chapter 13. Native SQL Queries"/><meta xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body><p xmlns:d="http://docbook.org/ns/docbook" id="title"><a href="http://www.hibernate.org" class="site_href"><strong>Hibernate.org</strong></a><a href="http://hibernate.org/Documentation/DocumentationOverview" class="doc_href"><strong>Community Documentation</strong></a></p><ul xmlns:d="http://docbook.org/ns/docbook" class="docnav"><li class="previous"><a accesskey="p" href="ch11.html"><strong>Prev</strong></a></li><li class="next"><a accesskey="n" href="ch13.html"><strong>Next</strong></a></li></ul><div class="chapter" title="Chapter 12. Criteria"><div class="titlepage"><div><div><h2 class="title"><a id="d5e3364"/>Chapter 12. Criteria</h2></div></div></div><div class="toc"><p><strong>Table of Contents</strong></p><dl><dt><span class="section"><a href="ch12.html#querycriteria-typedquery">12.1. Typed criteria queries</a></span></dt><dd><dl><dt><span class="section"><a href="ch12.html#querycriteria-typedquery-entity">12.1.1. Selecting an entity</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-typedquery-expression">12.1.2. Selecting an expression</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-typedquery-multiselect">12.1.3. Selecting multiple values</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-typedquery-construct">12.1.4. Selecting a wrapper</a></span></dt></dl></dd><dt><span class="section"><a href="ch12.html#querycriteria-tuple">12.2. Tuple criteria queries</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-from">12.3. FROM clause</a></span></dt><dd><dl><dt><span class="section"><a href="ch12.html#querycriteria-from-root">12.3.1. Roots</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-from-join">12.3.2. Joins</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-from-fetch">12.3.3. Fetches</a></span></dt></dl></dd><dt><span class="section"><a href="ch12.html#querycriteria-path">12.4. Path expressions</a></span></dt><dt><span class="section"><a href="ch12.html#querycriteria-param">12.5. Using parameters</a></span></dt></dl></div>
  5. <p>
  6. Criteria queries offer a type-safe alternative to HQL, JPQL and native-sql queries.
  7. </p>
  8. <div xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h2>Important</h2>
  9. <p>
  10. Hibernate offers an older, legacy <code class="interfacename">org.hibernate.Criteria</code> API which should be
  11. considered deprecated. No feature development will target those APIs. Eventually, Hibernate-specific
  12. criteria features will be ported as extensions to the JPA
  13. <code class="interfacename">javax.persistence.criteria.CriteriaQuery</code>. For details on the
  14. <code class="interfacename">org.hibernate.Criteria</code> API, see <a class="xref" href="">???</a>.
  15. </p>
  16. <p>
  17. This chapter will focus on the JPA APIs for declaring type-safe criteria queries.
  18. </p>
  19. </div>
  20. <p>
  21. Criteria queries are a programmatic, type-safe way to express a query. They are type-safe in terms of
  22. using interfaces and classes to represent various structural parts of a query such as the query itself,
  23. or the select clause, or an order-by, etc. They can also be type-safe in terms of referencing attributes
  24. as we will see in a bit. Users of the older Hibernate <code class="interfacename">org.hibernate.Criteria</code>
  25. query API will recognize the general approach, though we believe the JPA API to be superior
  26. as it represents a clean look at the lessons learned from that API.
  27. </p>
  28. <p>
  29. Criteria queries are essentially an object graph, where each part of the graph represents an increasing
  30. (as we navigate down this graph) more atomic part of query. The first step in performing a criteria query
  31. is building this graph. The <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code>
  32. interface is the first thing with which you need to become acquainted to begin using criteria queries. Its
  33. role is that of a factory for all the individual pieces of the criteria. You obtain a
  34. <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code> instance by calling the
  35. <code class="methodname">getCriteriaBuilder</code> method of either
  36. <code class="interfacename">javax.persistence.EntityManagerFactory</code> or
  37. <code class="interfacename">javax.persistence.EntityManager</code>.
  38. </p>
  39. <p>
  40. The next step is to obtain a <code class="interfacename">javax.persistence.criteria.CriteriaQuery</code>. This
  41. is accomplished using one of the 3 methods on
  42. <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code> for this purpose:
  43. </p>
  44. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">&lt;T&gt; CriteriaQuery&lt;T&gt; createQuery(Class&lt;T&gt; resultClass);
  45. CriteriaQuery&lt;Tuple&gt; createTupleQuery();
  46. CriteriaQuery&lt;Object&gt; createQuery();
  47. </pre>
  48. <p>
  49. Each serves a different purpose depending on the expected type of the query results.
  50. </p>
  51. <div xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h2>Note</h2>
  52. <p>
  53. <em class="citetitle">Chapter 6 Criteria API</em> of the JPA Specification
  54. already contains a decent amount of reference material pertaining to the various parts of a
  55. criteria query. So rather than duplicate all that content here, lets instead look at some of
  56. the more widely anticipated usages of the API.
  57. </p>
  58. </div>
  59. <div class="section" title="12.1. Typed criteria queries"><div class="titlepage"><div><div><h2 class="title"><a id="querycriteria-typedquery"/>12.1. Typed criteria queries</h2></div></div></div>
  60. <p>
  61. The type of the criteria query (aka the &lt;T&gt;) indicates the expected types in the query
  62. result. This might be an entity, an Integer, or any other object.
  63. </p>
  64. <div class="section" title="12.1.1. Selecting an entity"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-typedquery-entity"/>12.1.1. Selecting an entity</h3></div></div></div>
  65. <p>
  66. This is probably the most common form of query. The application wants to select entity instances.
  67. </p>
  68. <div class="example"><a id="ex-criteria-typedquery-entity"/><p class="title"><strong>Example 12.1. Selecting the root entity</strong></p><div class="example-contents">
  69. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Person&gt; criteria = builder.createQuery( Person.class );
  70. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  71. criteria.select( personRoot );
  72. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
  73. List&lt;Person&gt; people = em.createQuery( criteria ).getResultList();
  74. for ( Person person : people ) {
  75. ...
  76. }</pre>
  77. </div></div><br class="example-break"/>
  78. <p>
  79. The example uses <code class="methodname">createQuery</code> passing in the <code class="classname">Person</code>
  80. class reference as the results of the query will be Person objects.
  81. </p>
  82. <div xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h2>Note</h2>
  83. <p>
  84. The call to the <code class="methodname">CriteriaQuery.select</code> method in this example is
  85. unnecessary because <span class="emphasis"><em>personRoot</em></span> will be the implied selection since we
  86. have only a single query root. It was done here only for completeness of an example.
  87. </p>
  88. <p>
  89. The <span class="emphasis"><em>Person_.eyeColor</em></span> reference is an example of the static form of JPA
  90. metamodel reference. We will use that form exclusively in this chapter. See
  91. the documentation for the Hibernate JPA Metamodel Generator for additional details on
  92. the JPA static metamodel.
  93. </p>
  94. </div>
  95. </div>
  96. <div class="section" title="12.1.2. Selecting an expression"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-typedquery-expression"/>12.1.2. Selecting an expression</h3></div></div></div>
  97. <p>
  98. The simplest form of selecting an expression is selecting a particular attribute from an entity.
  99. But this expression might also represent an aggregation, a mathematical operation, etc.
  100. </p>
  101. <div class="example"><a id="ex-criteria-typedquery-attribute"/><p class="title"><strong>Example 12.2. Selecting an attribute</strong></p><div class="example-contents">
  102. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Integer&gt; criteria = builder.createQuery( Integer.class );
  103. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  104. criteria.select( personRoot.get( Person_.age ) );
  105. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
  106. List&lt;Integer&gt; ages = em.createQuery( criteria ).getResultList();
  107. for ( Integer age : ages ) {
  108. ...
  109. }</pre>
  110. </div></div><br class="example-break"/>
  111. <p>
  112. In this example, the query is typed as <code class="classname">java.lang.Integer</code> because that
  113. is the anticipated type of the results (the type of the <code class="methodname">Person#age</code> attribute
  114. is <code class="classname">java.lang.Integer</code>). Because a query might contain multiple references to
  115. the Person entity, attribute references always need to be qualified. This is accomplished by the
  116. <code class="methodname">Root#get</code> method call.
  117. </p>
  118. </div>
  119. <div class="section" title="12.1.3. Selecting multiple values"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-typedquery-multiselect"/>12.1.3. Selecting multiple values</h3></div></div></div>
  120. <p>
  121. There are actually a few different ways to select multiple values using criteria queries. We
  122. will explore 2 options here, but an alternative recommended approach is to use tuples as described in
  123. <a class="xref" href="ch12.html#querycriteria-tuple" title="12.2. Tuple criteria queries">Section 12.2, Tuple criteria queries</a>. Or consider a wrapper query; see
  124. <a class="xref" href="ch12.html#querycriteria-typedquery-construct" title="12.1.4. Selecting a wrapper">Section 12.1.4, Selecting a wrapper</a> for details.
  125. </p>
  126. <div class="example"><a id="ex-criteria-typedquery-array"/><p class="title"><strong>Example 12.3. Selecting an array</strong></p><div class="example-contents">
  127. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Object[]&gt; criteria = builder.createQuery( Object[].class );
  128. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  129. Path&lt;Long&gt; idPath = personRoot.get( Person_.id );
  130. Path&lt;Integer&gt; agePath = personRoot.get( Person_.age );
  131. criteria.select( builder.array( idPath, agePath ) );
  132. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
  133. List&lt;Object[]&gt; valueArray = em.createQuery( criteria ).getResultList();
  134. for ( Object[] values : valueArray ) {
  135. final Long id = (Long) values[0];
  136. final Integer age = (Integer) values[1];
  137. ...
  138. }</pre>
  139. </div></div><br class="example-break"/>
  140. <p>
  141. Technically this is classified as a typed query, but you can see from handling the results that
  142. this is sort of misleading. Anyway, the expected result type here is an array.
  143. </p>
  144. <p>
  145. The example then uses the <code class="methodname">array</code> method of
  146. <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code> which explicitly
  147. combines individual selections into a
  148. <code class="interfacename">javax.persistence.criteria.CompoundSelection</code>.
  149. </p>
  150. <div class="example"><a id="ex-criteria-typedquery-array2"/><p class="title"><strong>Example 12.4. Selecting an array (2)</strong></p><div class="example-contents">
  151. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Object[]&gt; criteria = builder.createQuery( Object[].class );
  152. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  153. Path&lt;Long&gt; idPath = personRoot.get( Person_.id );
  154. Path&lt;Integer&gt; agePath = personRoot.get( Person_.age );
  155. criteria.multiselect( idPath, agePath );
  156. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
  157. List&lt;Object[]&gt; valueArray = em.createQuery( criteria ).getResultList();
  158. for ( Object[] values : valueArray ) {
  159. final Long id = (Long) values[0];
  160. final Integer age = (Integer) values[1];
  161. ...
  162. }</pre>
  163. </div></div><br class="example-break"/>
  164. <p>
  165. Just as we saw in <a class="xref" href="ch12.html#ex-criteria-typedquery-array" title="Example 12.3. Selecting an array">Example 12.3, Selecting an array</a> we have a typed criteria
  166. query returning an Object array. Both queries are functionally equivalent. This second example
  167. uses the <code class="methodname">multiselect</code> method which behaves slightly differently based on
  168. the type given when the criteria query was first built, but in this case it says to select and
  169. return an <span class="emphasis"><em>Object[]</em></span>.
  170. </p>
  171. </div>
  172. <div class="section" title="12.1.4. Selecting a wrapper"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-typedquery-construct"/>12.1.4. Selecting a wrapper</h3></div></div></div>
  173. <p>Another alternative to <a class="xref" href="ch12.html#querycriteria-typedquery-multiselect" title="12.1.3. Selecting multiple values">Section 12.1.3, Selecting multiple values</a> is to instead
  174. select an object that will <span class="quote"><span class="quote">wrap</span></span> the multiple values. Going back to the example
  175. query there, rather than returning an array of <span class="emphasis"><em>[Person#id, Person#age]</em></span>
  176. instead declare a class that holds these values and instead return that.
  177. </p>
  178. <div class="example"><a id="ex-criteria-typedquery-construct"/><p class="title"><strong>Example 12.5. Selecting an wrapper</strong></p><div class="example-contents">
  179. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">public class PersonWrapper {
  180. private final Long id;
  181. private final Integer age;
  182. public PersonWrapper(Long id, Integer age) {
  183. this.id = id;
  184. this.age = age;
  185. }
  186. ...
  187. }
  188. ...
  189. CriteriaQuery&lt;PersonWrapper&gt; criteria = builder.createQuery( PersonWrapper.class );
  190. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  191. criteria.select(
  192. builder.construct(
  193. PersonWrapper.class,
  194. personRoot.get( Person_.id ),
  195. personRoot.get( Person_.age )
  196. )
  197. );
  198. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
  199. List&lt;PersonWrapper&gt; people = em.createQuery( criteria ).getResultList();
  200. for ( PersonWrapper person : people ) {
  201. ...
  202. }</pre>
  203. </div></div><br class="example-break"/>
  204. <p>
  205. First we see the simple definition of the wrapper object we will be using to wrap our result
  206. values. Specifically notice the constructor and its argument types. Since we will be returning
  207. <code class="classname">PersonWrapper</code> objects, we use <code class="classname">PersonWrapper</code> as the
  208. type of our criteria query.
  209. </p>
  210. <p>
  211. This example illustrates the use of the
  212. <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code> method
  213. <code class="methodname">construct</code> which is used to build a wrapper expression. For every row in the
  214. result we are saying we would like a <span class="emphasis"><em>PersonWrapper</em></span> instantiated with
  215. the remaining arguments by the matching constructor. This wrapper expression is then passed as
  216. the select.
  217. </p>
  218. </div>
  219. </div>
  220. <div class="section" title="12.2. Tuple criteria queries"><div class="titlepage"><div><div><h2 class="title"><a id="querycriteria-tuple"/>12.2. Tuple criteria queries</h2></div></div></div>
  221. <p>
  222. A better approach to <a class="xref" href="ch12.html#querycriteria-typedquery-multiselect" title="12.1.3. Selecting multiple values">Section 12.1.3, Selecting multiple values</a> is to use either a
  223. wrapper (which we just saw in <a class="xref" href="ch12.html#querycriteria-typedquery-construct" title="12.1.4. Selecting a wrapper">Section 12.1.4, Selecting a wrapper</a>) or using the
  224. <code class="interfacename">javax.persistence.Tuple</code> contract.
  225. </p>
  226. <div class="example"><a id="ex-criteria-typedquery-tuple"/><p class="title"><strong>Example 12.6. Selecting a tuple</strong></p><div class="example-contents">
  227. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Tuple&gt; criteria = builder.createTupleQuery();
  228. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  229. Path&lt;Long&gt; idPath = personRoot.get( Person_.id );
  230. Path&lt;Integer&gt; agePath = personRoot.get( Person_.age );
  231. criteria.multiselect( idPath, agePath );
  232. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
  233. List&lt;Tuple&gt; tuples = em.createQuery( criteria ).getResultList();
  234. for ( Tuple tuple : valueArray ) {
  235. assert tuple.get( 0 ) == tuple.get( idPath );
  236. assert tuple.get( 1 ) == tuple.get( agePath );
  237. ...
  238. }</pre>
  239. </div></div><br class="example-break"/>
  240. <p>
  241. This example illustrates accessing the query results through the
  242. <code class="interfacename">javax.persistence.Tuple</code> interface. The example uses the explicit
  243. <code class="methodname">createTupleQuery</code> of
  244. <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code>. An alternate approach
  245. is to use <code class="methodname">createQuery</code> passing <code class="literal">Tuple.class</code>.
  246. </p>
  247. <p>
  248. Again we see the use of the <code class="methodname">multiselect</code> method, just like in
  249. <a class="xref" href="ch12.html#ex-criteria-typedquery-array2" title="Example 12.4. Selecting an array (2)">Example 12.4, Selecting an array (2)</a>. The difference here is that the type of the
  250. <code class="interfacename">javax.persistence.criteria.CriteriaQuery</code> was defined as
  251. <code class="interfacename">javax.persistence.Tuple</code> so the compound selections in this case are
  252. interpreted to be the tuple elements.
  253. </p>
  254. <p>
  255. The <code class="interfacename">javax.persistence.Tuple</code> contract provides 3 forms of access to
  256. the underlying elements:
  257. </p>
  258. <div class="variablelist"><dl><dt><span class="term">typed</span></dt><dd>
  259. <p>
  260. The <a class="xref" href="ch12.html#ex-criteria-typedquery-tuple" title="Example 12.6. Selecting a tuple">Example 12.6, Selecting a tuple</a> example illustrates this form of access
  261. in the <code class="literal">tuple.get( idPath )</code> and <code class="literal">tuple.get( agePath )</code> calls.
  262. This allows typed access to the underlying tuple values based on the
  263. <code class="interfacename">javax.persistence.TupleElement</code> expressions used to build
  264. the criteria.
  265. </p>
  266. </dd><dt><span class="term">positional</span></dt><dd>
  267. <p>
  268. Allows access to the underlying tuple values based on the position. The simple
  269. <span class="emphasis"><em>Object get(int position)</em></span> form is very similar to the access
  270. illustrated in <a class="xref" href="ch12.html#ex-criteria-typedquery-array" title="Example 12.3. Selecting an array">Example 12.3, Selecting an array</a> and
  271. <a class="xref" href="ch12.html#ex-criteria-typedquery-array2" title="Example 12.4. Selecting an array (2)">Example 12.4, Selecting an array (2)</a>. The
  272. <span class="emphasis"><em>&lt;X&gt; X get(int position, Class&lt;X&gt; type</em></span> form
  273. allows typed positional access, but based on the explicitly supplied type which the tuple
  274. value must be type-assignable to.
  275. </p>
  276. </dd><dt><span class="term">aliased</span></dt><dd>
  277. <p>
  278. Allows access to the underlying tuple values based an (optionally) assigned alias. The
  279. example query did not apply an alias. An alias would be applied via the
  280. <code class="methodname">alias</code> method on
  281. <code class="interfacename">javax.persistence.criteria.Selection</code>. Just like
  282. <code class="literal">positional</code> access, there is both a typed
  283. (<span class="emphasis"><em>Object get(String alias)</em></span>) and an untyped
  284. (<span class="emphasis"><em>&lt;X&gt; X get(String alias, Class&lt;X&gt; type</em></span> form.
  285. </p>
  286. </dd></dl></div>
  287. </div>
  288. <div class="section" title="12.3. FROM clause"><div class="titlepage"><div><div><h2 class="title"><a id="querycriteria-from"/>12.3. FROM clause</h2></div></div></div>
  289. <div class="blockquote"><table border="0" width="100%" cellspacing="0" cellpadding="0" class="blockquote" summary="Block quote"><tr><td width="10%" valign="top"> </td><td width="80%" valign="top"><p>
  290. A CriteriaQuery object defines a query over one or more entity, embeddable, or basic abstract
  291. schema types. The root objects of the query are entities, from which the other types are reached
  292. by navigation.
  293. </p></td><td width="10%" valign="top"> </td></tr><tr><td width="10%" valign="top"> </td><td colspan="2" align="right" valign="top">--<span class="attribution"><em class="citetitle">JPA Specification</em>, section 6.5.2 Query Roots, pg 262</span></td></tr></table></div>
  294. <div xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h2>Note</h2>
  295. <p>
  296. All the individual parts of the FROM clause (roots, joins, paths) implement the
  297. <code class="interfacename">javax.persistence.criteria.From</code> interface.
  298. </p>
  299. </div>
  300. <div class="section" title="12.3.1. Roots"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-from-root"/>12.3.1. Roots</h3></div></div></div>
  301. <p>
  302. Roots define the basis from which all joins, paths and attributes are available in the query.
  303. A root is always an entity type. Roots are defined and added to the criteria by the overloaded
  304. <code class="methodname">from</code> methods on
  305. <code class="interfacename">javax.persistence.criteria.CriteriaQuery</code>:
  306. </p>
  307. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">&lt;X&gt; Root&lt;X&gt; from(Class&lt;X&gt;);
  308. &lt;X&gt; Root&lt;X&gt; from(EntityType&lt;X&gt;)</pre>
  309. <div class="example"><a id="d5e3518"/><p class="title"><strong>Example 12.7. Adding a root</strong></p><div class="example-contents">
  310. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Person&gt; personCriteria = builder.createQuery( Person.class );
  311. // create and add the root
  312. person.from( Person.class );</pre>
  313. </div></div><br class="example-break"/>
  314. <p>
  315. Criteria queries may define multiple roots, the effect of which is to create a cartesian
  316. product between the newly added root and the others. Here is an example matching all single
  317. men and all single women:
  318. </p>
  319. <div class="example"><a id="d5e3522"/><p class="title"><strong>Example 12.8. Adding multiple roots</strong></p><div class="example-contents">
  320. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery query = builder.createQuery();
  321. Root&lt;Person&gt; men = query.from( Person.class );
  322. Root&lt;Person&gt; women = query.from( Person.class );
  323. Predicate menRestriction = builder.and(
  324. builder.equal( men.get( Person_.gender ), Gender.MALE ),
  325. builder.equal( men.get( Person_.relationshipStatus ), RelationshipStatus.SINGLE )
  326. );
  327. Predicate womenRestriction = builder.and(
  328. builder.equal( women.get( Person_.gender ), Gender.FEMALE ),
  329. builder.equal( women.get( Person_.relationshipStatus ), RelationshipStatus.SINGLE )
  330. );
  331. query.where( builder.and( menRestriction, womenRestriction ) );</pre>
  332. </div></div><br class="example-break"/>
  333. </div>
  334. <div class="section" title="12.3.2. Joins"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-from-join"/>12.3.2. Joins</h3></div></div></div>
  335. <p>
  336. Joins allow navigation from other <code class="interfacename">javax.persistence.criteria.From</code>
  337. to either association or embedded attributes. Joins are created by the numerous overloaded
  338. <code class="methodname">join</code> methods of the
  339. <code class="interfacename">javax.persistence.criteria.From</code> interface
  340. </p>
  341. <div class="example"><a id="criteria-join-singular"/><p class="title"><strong>Example 12.9. Example with Embedded and ManyToOne</strong></p><div class="example-contents">
  342. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Person&gt; personCriteria = builder.createQuery( Person.class );
  343. Root&lt;Person&gt; personRoot = person.from( Person.class );
  344. // Person.address is an embedded attribute
  345. Join&lt;Person,Address&gt; personAddress = personRoot.join( Person_.address );
  346. // Address.country is a ManyToOne
  347. Join&lt;Address,Country&gt; addressCountry = personAddress.join( Address_.country );</pre>
  348. </div></div><br class="example-break"/>
  349. <div class="example"><a id="criteria-join-plural"/><p class="title"><strong>Example 12.10. Example with Collections</strong></p><div class="example-contents">
  350. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Person&gt; personCriteria = builder.createQuery( Person.class );
  351. Root&lt;Person&gt; personRoot = person.from( Person.class );
  352. Join&lt;Person,Order&gt; orders = personRoot.join( Person_.orders );
  353. Join&lt;Order,LineItem&gt; orderLines = orders.join( Order_.lineItems );</pre>
  354. </div></div><br class="example-break"/>
  355. </div>
  356. <div class="section" title="12.3.3. Fetches"><div class="titlepage"><div><div><h3 class="title"><a id="querycriteria-from-fetch"/>12.3.3. Fetches</h3></div></div></div>
  357. <p>
  358. Just like in HQL and JPQL, criteria queries can specify that associated data be fetched along
  359. with the owner. Fetches are created by the numerous overloaded <code class="methodname">fetch</code>
  360. methods of the <code class="interfacename">javax.persistence.criteria.From</code> interface.
  361. </p>
  362. <div class="example"><a id="criteria-fetch-singular"/><p class="title"><strong>Example 12.11. Example with Embedded and ManyToOne</strong></p><div class="example-contents">
  363. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Person&gt; personCriteria = builder.createQuery( Person.class );
  364. Root&lt;Person&gt; personRoot = person.from( Person.class );
  365. // Person.address is an embedded attribute
  366. Fetch&lt;Person,Address&gt; personAddress = personRoot.fetch( Person_.address );
  367. // Address.country is a ManyToOne
  368. Fetch&lt;Address,Country&gt; addressCountry = personAddress.fetch( Address_.country );</pre>
  369. </div></div><br class="example-break"/>
  370. <div xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h2>Note</h2>
  371. <p>
  372. Technically speaking, embedded attributes are always fetched with their owner. However in
  373. order to define the fetching of <span class="emphasis"><em>Address#country</em></span> we needed a
  374. <code class="interfacename">javax.persistence.criteria.Fetch</code> for its parent path.
  375. </p>
  376. </div>
  377. <div class="example"><a id="criteria-fetch-plural"/><p class="title"><strong>Example 12.12. Example with Collections</strong></p><div class="example-contents">
  378. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&amp;lt;Person&amp;gt; personCriteria = builder.createQuery( Person.class );
  379. Root&lt;Person&gt; personRoot = person.from( Person.class );
  380. Fetch&lt;Person,Order&gt; orders = personRoot.fetch( Person_.orders );
  381. Fetch&lt;Order,LineItem&gt; orderLines = orders.fetch( Order_.lineItems );</pre>
  382. </div></div><br class="example-break"/>
  383. </div>
  384. </div>
  385. <div class="section" title="12.4. Path expressions"><div class="titlepage"><div><div><h2 class="title"><a id="querycriteria-path"/>12.4. Path expressions</h2></div></div></div>
  386. <div xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h2>Note</h2>
  387. <p>
  388. Roots, joins and fetches are themselves paths as well.
  389. </p>
  390. </div>
  391. </div>
  392. <div class="section" title="12.5. Using parameters"><div class="titlepage"><div><div><h2 class="title"><a id="querycriteria-param"/>12.5. Using parameters</h2></div></div></div>
  393. <div class="example"><a id="ex-querycriteria-param"/><p class="title"><strong>Example 12.13. Using parameters</strong></p><div class="example-contents">
  394. <pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">CriteriaQuery&lt;Person&gt; criteria = build.createQuery( Person.class );
  395. Root&lt;Person&gt; personRoot = criteria.from( Person.class );
  396. criteria.select( personRoot );
  397. ParameterExpression&lt;String&gt; eyeColorParam = builder.parameter( String.class );
  398. criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), eyeColorParam ) );
  399. TypedQuery&lt;Person&gt; query = em.createQuery( criteria );
  400. query.setParameter( eyeColorParam, "brown" );
  401. List&lt;Person&gt; people = query.getResultList();</pre>
  402. </div></div><br class="example-break"/>
  403. <p>
  404. Use the <code class="methodname">parameter</code> method of
  405. <code class="interfacename">javax.persistence.criteria.CriteriaBuilder</code> to obtain a parameter
  406. reference. Then use the parameter reference to bind the parameter value to the
  407. <code class="interfacename">javax.persistence.Query</code>
  408. </p>
  409. </div>
  410. </div><hr xmlns="" xmlns:d="http://docbook.org/ns/docbook"/><a xmlns="" xmlns:d="http://docbook.org/ns/docbook" href="legalnotice.html"/><ul xmlns:d="http://docbook.org/ns/docbook" class="docnav"><li class="previous"><a accesskey="p" href="ch11.html"><strong>Prev</strong>Chapter 11. HQL and JPQL</a></li><li class="up"><a accesskey="u" href="#"><strong>Up</strong></a></li><li class="home"><a accesskey="h" href="index.html"><strong>Home</strong></a></li><li class="next"><a accesskey="n" href="ch13.html"><strong>Next</strong>Chapter 13. Native SQL Queries</a></li></ul></body></html>