PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/cayenne-3.0.1/docs/doc/src/main/resources/doc/Documentation/Cayenne Guide/Queries/SelectQuery/Using Orderings/index.html

https://gitlab.com/essere.lab.public/qualitas.class-corpus
HTML | 142 lines | 106 code | 18 blank | 18 comment | 0 complexity | 7181b183363d685b441b17ab7f38fc08 MD5 | raw file
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. -->
  17. <html>
  18. <head>
  19. <title>Apache Cayenne Documentation - Using Orderings</title>
  20. <style type="text/css">@import "../../../../../style.css";</style>
  21. </head>
  22. <body>
  23. <div class="header">
  24. <div style="float: left;"><a href="http://cayenne.apache.org/"><img src="../../../../../images/logo.gif" align="absmiddle" border="0"></a></div>
  25. <span class="logoSpaceLink"><a href="../../../../../index.html">Cayenne User Documentation</a></span><br />
  26. <span class="pagetitle">Using Orderings</span>
  27. </div>
  28. <div id="cayenne_toc">
  29. <ul>
  30. <li><a href="../../../../../Documentation/Cayenne Guide/Introduction/index.html">Introduction</a></li>
  31. <li><a href="../../../../../Documentation/Cayenne Guide/Installation/index.html">Installation</a></li>
  32. <li><a href="../../../../../Documentation/Cayenne Guide/Tutorial/index.html">Tutorial</a></li>
  33. <li><a href="../../../../../Documentation/Cayenne Guide/Design/index.html">Design</a></li>
  34. <li><a href="../../../../../Documentation/Cayenne Guide/DataContext/index.html">DataContext</a></li>
  35. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/index.html">Queries</a><ul>
  36. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/SelectQuery/index.html">SelectQuery</a><ul>
  37. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/SelectQuery/Parameterized Queries/index.html">Parameterized Queries</a></li>
  38. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/SelectQuery/Qualifier Expressions/index.html">Qualifier Expressions</a></li>
  39. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/SelectQuery/Using Orderings/index.html">Using Orderings</a><ul>
  40. </ul>
  41. </li>
  42. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/SelectQuery/SelectQuery Customization/index.html">SelectQuery Customization</a></li>
  43. </ul>
  44. </li>
  45. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/SQLTemplate Query/index.html">SQLTemplate Query</a></li>
  46. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/EJBQLQuery/index.html">EJBQLQuery</a></li>
  47. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/NamedQuery/index.html">NamedQuery</a></li>
  48. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/ObjectIdQuery/index.html">ObjectIdQuery</a></li>
  49. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/QueryChain/index.html">QueryChain</a></li>
  50. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/RelationshipQuery/index.html">RelationshipQuery</a></li>
  51. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/Queries Stored in DataMap/index.html">Queries Stored in DataMap</a></li>
  52. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/Caching Query Results/index.html">Caching Query Results</a></li>
  53. <li><a href="../../../../../Documentation/Cayenne Guide/Queries/Customizing Queries/index.html">Customizing Queries</a></li>
  54. </ul>
  55. </li>
  56. <li><a href="../../../../../Documentation/Cayenne Guide/DataObjects/index.html">DataObjects</a></li>
  57. <li><a href="../../../../../Documentation/Cayenne Guide/Stored Procedures/index.html">Stored Procedures</a></li>
  58. <li><a href="../../../../../Documentation/Cayenne Guide/Expressions/index.html">Expressions</a></li>
  59. <li><a href="../../../../../Documentation/Cayenne Guide/Lifecycle Callbacks/index.html">Lifecycle Callbacks</a></li>
  60. <li><a href="../../../../../Documentation/Cayenne Guide/Performance Tuning/index.html">Performance Tuning</a></li>
  61. <li><a href="../../../../../Documentation/Cayenne Guide/Caching and Fresh Data/index.html">Caching and Fresh Data</a></li>
  62. <li><a href="../../../../../Documentation/Cayenne Guide/Deployment/index.html">Deployment</a></li>
  63. <li><a href="../../../../../Documentation/Cayenne Guide/Ant Tasks/index.html">Ant Tasks</a></li>
  64. <li><a href="../../../../../Documentation/Cayenne Guide/Maven2 Plugins/index.html">Maven2 Plugins</a></li>
  65. <li><a href="../../../../../Documentation/Cayenne Guide/Customization/index.html">Customization</a></li>
  66. </ul>
  67. </div>
  68. <div id="ConfluenceContent"><p>To sort SelectQuery results, Orderings are used. Orderings use path expressions discussed in the previous section to identify the attributes that must be used in sorting. Orderings also use a SortOrder to identify how the ordering should be performed.</p>
  69. <p>There are 4 SortOrder options:</p>
  70. <ul>
  71. <li>ASCENDING (ascending order, case &#8211; or database &#8211; sensitive)</li>
  72. <li>ASCENDING_INSENSITIVE (ascending order, case-insensitive)</li>
  73. <li>DESCENDING (descending order, case &#8211; or database &#8211; sensitive)</li>
  74. <li>DESCENDING_INSENSITIVE (descending order, case-insensitive)</li>
  75. </ul>
  76. <p>To order results by artist name, the following code can be used:</p>
  77. <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
  78. <pre class="code-java"><span class="code-keyword">import</span> org.apache.cayenne.query.SelectQuery;
  79. <span class="code-keyword">import</span> org.apache.cayenne.query.SortOrder;
  80. ...
  81. SelectQuery query = <span class="code-keyword">new</span> SelectQuery(<span class="code-quote">"Artist"</span>);
  82. <span class="code-comment">// add ordering by Artist name:
  83. </span>query.addOrdering(<span class="code-quote">"artistName"</span>, SortOrder.ASCENDING);
  84. </pre>
  85. </div></div>
  86. <div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="../../../../../images/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Legacy Information</b><br />In Cayenne versions prior to 3.0, the addOrdering() call in the above example would appear as <tt>query.addOrdering("artistName", true)</tt>. A boolean parameter of true meant to order ascending (false descending).</td></tr></table></div>
  87. <p>Orderings also support in-memory sorting of lists of Java Beans (all DataObjects are normally Java Beans, since they has set/get method pairs for all the properties). For instance to sort with a single ordering, the following code might be used:</p>
  88. <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
  89. <pre class="code-java"><span class="code-keyword">import</span> org.apache.cayenne.query.Ordering;
  90. <span class="code-keyword">import</span> org.apache.cayenne.query.SortOrder;
  91. ...
  92. <span class="code-comment">// assume <span class="code-keyword">this</span> is a properly initialized list of Artists
  93. </span>List list = ...;
  94. <span class="code-comment">// creates asending ordering by Artist name
  95. </span>Ordering ordering = <span class="code-keyword">new</span> Ordering(<span class="code-quote">"artistName"</span>, SortOrder.ASCENDING);
  96. <span class="code-comment">// orders a list
  97. </span>ordering.orderList(list);
  98. </pre>
  99. </div></div>
  100. <div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="../../../../../images/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Legacy Information</b><br />In Cayenne versions prior to 3.0, the Ordering() constructor in the above example would appear as: <tt>new Ordering("artistName", true)</tt>. A boolean parameter of true meant to order ascending (false descending).</td></tr></table></div>
  101. <p>If there is a need to sort on more than one object property, multiple Orderings can be passed as a List to a static method <tt>orderList(List, List)</tt>. The cost of adding new Orderings decreases, as the list of objects ends up being sorted by the first Ordering, then, if any two objects are equal for first Ordering, by the second, and so on.</p>
  102. <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
  103. <pre class="code-java"><span class="code-keyword">import</span> org.apache.cayenne.query.Ordering;
  104. <span class="code-keyword">import</span> org.apache.cayenne.query.SortOrder;
  105. ...
  106. <span class="code-comment">// assume <span class="code-keyword">this</span> is a properly initialized list of Paintings
  107. </span>List list = ...;
  108. List orderings = <span class="code-keyword">new</span> ArrayList();
  109. orderings.add(<span class="code-keyword">new</span> Ordering(<span class="code-quote">"paintingTitle"</span>, SortOrder.ASCENDING));
  110. orderings.add(<span class="code-keyword">new</span> Ordering(<span class="code-quote">"estimatedPrice"</span>, SortOrder.DESCENDING));
  111. <span class="code-comment">// orders a list aplying multiple orderings
  112. </span>Ordering.orderList(list, orderings);
  113. </pre>
  114. </div></div></div>
  115. </div>
  116. <div class="clearer">.</div>
  117. <div style="height: 12px; background-image: url('../../../../../images/border_bottom.gif'); background-repeat: repeat-x;"></div>
  118. <div class="smalltext copyright">
  119. Copyright &copy;2001-2010 Apache Software Foundation
  120. </div>
  121. </body>
  122. </html>