openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J Grassel <fyrew...@gmail.com>
Subject Re: @OrderBy not work?
Date Fri, 18 Sep 2009 16:35:19 GMT

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.

Mime
View raw message