cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Using Orderings
Date Fri, 09 Oct 2009 14:51:01 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAYDOC/Using+Orderings">Using
Orderings</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~blacknext">Michael
Gentry</a>
    </h4>
     Added a missing import statement.
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Added a missing import statement.<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <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>

<p>There are 4 SortOrder options:</p>

<ul>
	<li>ASCENDING (ascending order, case &#8211; or database &#8211; sensitive)</li>
	<li>ASCENDING_INSENSITIVE (ascending order, case-insensitive)</li>
	<li>DESCENDING (descending order, case &#8211; or database &#8211; sensitive)</li>
	<li>DESCENDING_INSESNITIVE (descending order, case-insensitive)</li>
</ul>


<p>To order results by artist name, the following code can be used:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.cayenne.query.SelectQuery;
<span class="code-keyword">import</span> org.apache.cayenne.query.SortOrder;
...
SelectQuery query = <span class="code-keyword">new</span> SelectQuery(<span
class="code-quote">"Artist"</span>);

<span class="code-comment">// add ordering by Artist name:
</span>query.addOrdering(<span class="code-quote">"artistName"</span>, SortOrder.ASCENDING);
</pre>
</div></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Legacy Information</b><br
/><p>In older versions of Cayenne (3.0M6 and before), the addOrdering() call in the
above example would appear as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
query.addOrdering(<span class="code-quote">"artistName"</span>, <span class="code-keyword">true</span>);
</pre>
</div></div>
<p>A boolean parameter of true meant to order ascending (false descending).</p></td></tr></table></div>

<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>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.cayenne.query.Ordering;
<span class="code-keyword">import</span> org.apache.cayenne.query.SortOrder;
...
<span class="code-comment">// assume <span class="code-keyword">this</span>
is a properly initialized list of Artists
</span>List list = ...; 

<span class="code-comment">// creates asending ordering by Artist name
</span>Ordering ordering = <span class="code-keyword">new</span> Ordering(<span
class="code-quote">"artistName"</span>, SortOrder.ASCENDING);

<span class="code-comment">// orders a list
</span>ordering.orderList(list);
</pre>
</div></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Legacy Information</b><br
/><p>In older versions of Cayenne (3.0M6 and before), the Ordering() constructor
in the above example would appear as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Ordering ordering = <span class="code-keyword">new</span> Ordering(<span class="code-quote">"artistName"</span>,
<span class="code-keyword">true</span>);
</pre>
</div></div>
<p>A boolean parameter of true meant to order ascending (false descending).</p></td></tr></table></div>

<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>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.cayenne.query.Ordering;
<span class="code-keyword">import</span> org.apache.cayenne.query.SortOrder;
...
<span class="code-comment">// assume <span class="code-keyword">this</span>
is a properly initialized list of Paintings
</span>List list = ...; 

List orderings = <span class="code-keyword">new</span> ArrayList();
orderings.add(<span class="code-keyword">new</span> Ordering(<span class="code-quote">"paintingTitle"</span>,
SortOrder.ASCENDING));
orderings.add(<span class="code-keyword">new</span> Ordering(<span class="code-quote">"estimatedPrice"</span>,
SortOrder.DESCENDING));

<span class="code-comment">// orders a list aplying multiple orderings
</span>Ordering.orderList(list, orderings);
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Using+Orderings">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=10530&revisedVersion=4&originalVersion=3">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Using+Orderings?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message