-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: @OrderedBy ignored for basic type collection
PostPosted: Sat Feb 05, 2011 11:35 am 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Hi,

I have an entity class that has a String collection. I want that collection to be ordered, so I used the @OrderBy annotation (from JPA).
The problem is that the annotation seems to be ignored, no matter what value I give to the annotation (even invalid ones):

  • @OrderBy
  • @OrderBy("ASC")
  • @OrderBy("DESC")
  • @OrderBy("some invalid order by clause")

Here is the entity class:

Code:
@Entity
@Table(name = "document_types")
public class DocumentType extends AbstractEntity {

   ...
   private Set<String> allowedAttachmentTypes = Sets.newLinkedHashSet();
   
        ....

   @ElementCollection(fetch = FetchType.EAGER)
   @CollectionTable(
      name = "allowed_attachment_types",
      joinColumns = {@JoinColumn(name = "document_type_id")}
   )
   @OrderBy
   @Column(name = "extension")
   public Set<String> getAllowedAttachmentTypes() {
      return allowedAttachmentTypes;
   }

        ...
}


Here is the DDL for the collection table:

Code:
CREATE TABLE allowed_attachment_types (
   
   document_type_id INT NOT NULL,
   extension VARCHAR(32) NOT NULL,
   
   PRIMARY KEY (document_type_id, extension),
   FOREIGN KEY (document_type_id) REFERENCES  document_types (id)
   
) ENGINE = InnoDB;


When an entity is fetched, the following SQL is ran:


Hibernate: select documentty0_.id as id2_, documentty0_.name as name2_ from document_types documentty0_ where documentty0_.id=?
Hibernate: select allowedatt0_.document_type_id as document1_2_0_, allowedatt0_.extension as extension0_ from allowed_attachment_types allowedatt0_ where allowedatt0_.document_type_id=?


As you can see, the SELECT for retrieving the allowedAttachmentTypes does not have an ORDER BY clause.

I could use a SortedSet or whatever, but I should still be able to use the annotation.

The JPA JavaDocs say:
Quote:
...
The OrderBy annotation may be applied to an element collection. When OrderBy is applied to an element collection of basic type, the ordering will be by value of the basic objects and the property or field name is not used.
...


Is this a bug or am I doing something wrong?

Hibernate version: 3.5.6
Database: MySQL


Top
 Profile  
 
 Post subject: Re: @OrderedBy ignored for basic type collection
PostPosted: Sat Feb 05, 2011 1:18 pm 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Hi,

I think you're confusing OrderBy with the OrderColumn annotation. The latter is for storing an order attribute in the database,
OrderBy is for sorting "in-memory" so you won't see anything special in the select statement.
But the ordering should take effect in the collection (maybe try a list first) after the select, did you actually check the ordering of the elements?

regards,

Robin.


Top
 Profile  
 
 Post subject: Re: @OrderedBy ignored for basic type collection
PostPosted: Sun Feb 06, 2011 1:09 pm 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
I'm not talking about the @OrderColumn annotation (that is used for specifying the column that keeps the index of a list for ordering purposes).
I'm not talking about the Hibernate's @Sort annotation either (that is used for in-memory collection sorting).

I am trying to use the @OrderBy, that should add an ORDER BY clause when fetching a collection.
I am using the @OrderBy for a one-to-many relationship backed collection and it works as it should. But it seems to be ignored on my basic type collection mapping.


Top
 Profile  
 
 Post subject: Re: @OrderedBy ignored for basic type collection
PostPosted: Mon Feb 07, 2011 6:34 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Ok, forget my comment that "you won't see anything special in the select statement", that's plain stupid. (I thought that a JPA implementation
is free to either use order by in the select statement or sort in memory but the more I think about it ... as I said - stupid!)

But I'm not sure what to expect in your case: you have a primary key (document_type_id, extension) and you're using
a collection of a basic type (String). See the following javadoc of @OrderBy:
"If the ordering element is not specified for an entity association, ordering by the primary key of the associated entity is assumed."
...
"When OrderBy is applied to an element collection of basic type, the ordering will be by value of the basic objects and the property or field name is not used. "

The word "assumed" leaves some room for interpretation but I also would expect an "order by extension" in your case, so maybe it's really a bug.
With Hibernate 3.6 I can confirm that the following does work and yields a "select .. order by position:

Code:
@ElementCollection
@CollectionTable(name = "vocalspictures", joinColumns = @JoinColumn(...))
@OrderBy("position")
protected List<VocalistPicture> pictures;

where VocalistPicture is an embedabble:
Code:
@Embeddable
@Access(AccessType.FIELD)
public class VocalistPicture {

    @Column(nullable = false, length = MAX_ID_LENGTH)
    protected String id;

    @Column(name = "\"position\"", nullable = false)
    protected int position;
...
}


Hope that helps!

Robin.


Top
 Profile  
 
 Post subject: Re: @OrderedBy ignored for basic type collection
PostPosted: Wed Feb 15, 2012 9:29 am 
Newbie

Joined: Tue Sep 27, 2005 7:30 am
Posts: 12
Try using
Code:
@OrderColumn


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.