Return-Path: Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: (qmail 46876 invoked from network); 18 Sep 2009 16:35:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 18 Sep 2009 16:35:52 -0000 Received: (qmail 19190 invoked by uid 500); 18 Sep 2009 16:35:52 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 19127 invoked by uid 500); 18 Sep 2009 16:35:51 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 19117 invoked by uid 99); 18 Sep 2009 16:35:51 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Sep 2009 16:35:51 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of lists+1214986160035-208411@n2.nabble.com designates 216.139.236.158 as permitted sender) Received: from [216.139.236.158] (HELO kuber.nabble.com) (216.139.236.158) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Sep 2009 16:35:41 +0000 Received: from tervel.nabble.com ([192.168.236.150]) by kuber.nabble.com with esmtp (Exim 4.63) (envelope-from ) id 1MogQZ-0005Kd-UW for users@openjpa.apache.org; Fri, 18 Sep 2009 09:35:19 -0700 Date: Fri, 18 Sep 2009 09:35:19 -0700 (PDT) From: J Grassel To: users@openjpa.apache.org Message-ID: <1253291719944-3670943.post@n2.nabble.com> In-Reply-To: <1253199478401-3663648.post@n2.nabble.com> References: <1253199478401-3663648.post@n2.nabble.com> Subject: Re: @OrderBy not work? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nabble-From: fyrewyld@gmail.com X-Virus-Checked: Checked by ClamAV on apache.org Hi there, Forrest. Could you illustrate the lead-up for when you are checking the list's order? I'm curious as to at which point in the program's life cycle you are expecting the collection to be sorted, as the JPA Spec states: 9.1.28 OrderBy Annotation The OrderBy annotation specifies the ordering of the elements of a collection valued association at the point when the association is retrieved. Which means when a Collection is freshly reconstituted from the database is when the elements are guaranteed to be ordered. This means that with a new entity (and hence, a new Collection object created by your entity class's constructor) there is no automatic ordering of elements. Presumably, it also means that adding new objects to the relationship does not automatically sort the new addition into its proper location (although the user-written application could manage this with methods such as List.add(int, Object). Some examples, based on my interpretation of the spec: 1) New Entity Entity e = new Entity(id); for (int i=0; i < n.length; i++) { // Assume that entites in n are either already persisted, or that the relationship is CASCADE.PERSIST|ALL e.getCollectionRelationship().add(n[i]); } // At this point, e.collectionRelationship is a user-instantiated Collection, so unless a Collection that automatically // sorts elements are used, then no particularly ordering is guaranteed unless the user application makes an effort // to sort the elements as they're inserted. // Begin tx em.persist(e); // Commit tx // e.collectionRelationship is still not automatically sorted, as the entity was just added to the db; its origination is // from the user application, and not a product of a DB fetch 2) Existing entity Entity e = em.find(e.class, x); // e.collectionRelationship, if this collection is annotated with an @OrderBy annotation, should be ordered by // pk or by fields specified by annotation // e.getCollectionRelationship().add(element) // A new element has been added to the collection, unless the user application makes an effort to insert the // element at the proper location, then the collection is likely out of order. The JPA spec states that ordering // is only performed when the association is retrieved from the database, it says nothing about enforcing the // ordering when mutation operations are performed. Forrest Xia wrote: > > Hi, > > I tried @OrderBy on a list field, but whatever I set it, it always sorts > the target entity by the identity. > > Code snip like this: > > @Entity > public class B { > @Id > private f1; > @Column > private f2; > @Column > private f3; > } > > @Entity > public class A { > @Id > private p1; > @Column > private p2; > @OneToMany > @OrderBy("f3 DESC") > private Collection p3; > } > > As the example, query p3, the return list's order is always by B's id "f1' > ascending, though I set to order by f3. Is it a known bug? > > I am using OpenJPA 1.2.1 bundled in Geronimo. Thanks for any comments! > > Regards, Forrest > -- View this message in context: http://n2.nabble.com/OrderBy-not-work-tp3663648p3670943.html Sent from the OpenJPA Users mailing list archive at Nabble.com.