-->
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.  [ 11 posts ] 
Author Message
 Post subject: @javax.persistence.OrderBy + Set does not order results
PostPosted: Wed Jan 17, 2007 6:08 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="ANIMAL_COUNTRY",
joinColumns=@JoinColumn(name="ANIMAL_ID"),
inverseJoinColumns=@JoinColumn(name="COUNTRY_ID"))
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@javax.persistence.OrderBy("name")
private Set<Country> countries;

Does not order the countries by name. If I change the Set to a List it correctly ordered. Any ideas? Does Hibernate fail to use a LinkedHashSet if the @javax.persistence.OrderBy annotation is used?

Mike


Top
 Profile  
 
 Post subject: Wrong Collection (I suppose..)
PostPosted: Wed Jan 17, 2007 6:53 pm 
Newbie

Joined: Wed Jan 17, 2007 6:42 pm
Posts: 1
Hi Mike,

I think the reason you're getting an unsorted collection with a Set and a sorted one with a List is just because they're naturally unsorted or sorted !

To get a sorted collection you have to use a SortedSet (e.g. a TreeSet)
or a List or even an array..

In your case,

private Set<Country> countries;

must be something like

private SortedSet<Country> countries = new TreeSet<Country>();

(of course, you can omit the assignment if you don't need it...)

Hope it helps !

bye

Andrea


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 7:00 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
Quote:
I think the reason you're getting an unsorted collection with a Set and a sorted one with a List is just because they're naturally unsorted or sorted !


I wouldn't say that Sets are naturally unsorted but I would say Lists are naturally sorted. The hibernate docs say that if I use a org.hibernate.annotations.OrderBy then Hibernate will internally use a LinkedHashSet which is an ordered set. However Hibernate doesn't appear to use a LinkedHashSet if I use the javax.persistence.OrderBy annotation.

I'd like to stay as close to JPA as possible just thought it strange that hibernate could provide ordered (not sorted) Set functionality with the hibernate @OrderBy annotation but not do the same with the JPA @OrderBy annotation. But maybe there is some strange JPA spec requirement that says the JPA @OrderBy annotation should only work on a List?

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 7:06 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
In fact reading through the spec (section 9.1.26) it just says "ordering of the elements of a collection" so I would think Set should be included in that.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 4:08 am 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
But the hibernate-annotation doc also states:

Quote:
@OrderBy currently works only on collections having no association table

see documentation


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 11:47 am 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
Ah, good find. Thanks.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 8:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The doc was not uptodate,
@o.h.a.OrderBy and j.p.OrderBy lead to the same underlying ordering mechanism, so the behavior should be the same.

Can you check if you have a LinkedHashSet underneath the PersistenceSet (through a debugger)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 6:25 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
When I debug it appears that when using j.p.OrderBy the PersistentSet.set is a HashSet and not a LinkedHashSet. Here is my Animal and Country models just to make sure it is not some kind of dumb annotation problem on my end:

Animal trimmed getters and setters
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@NamedQueries({
   @NamedQuery(name=Animal.QUERY_GET_ALL_ANIMALS_AS_SELECT_ITEMS, query="select new javax.faces.model.SelectItem(a, a.name) from Animal a")
})
@SuppressWarnings("serial")
public class Animal implements Serializable {
   public static final String QUERY_GET_ALL_ANIMALS_AS_SELECT_ITEMS = "getAllAnimalsAsSelectItems";

   @Id @GeneratedValue
   private Long id;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="CREATED", nullable = false, updatable = false)
   private Date created = new Date();
   @Column(unique=true, nullable=false)
    private String name;
    @Enumerated(EnumType.STRING)
    private Gender gender;
    @Column(length=4000)
    private String description;
    private String imageUrl;
    @Column(nullable=false)
    private double price;
    private Date dateOfBirth;
    @ManyToOne(fetch=FetchType.LAZY)
    private Classification classification;
    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="ANIMAL_COUNTRY",
          joinColumns=@JoinColumn(name="ANIMAL_ID"),
          inverseJoinColumns=@JoinColumn(name="COUNTRY_ID"))
    @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
    @BatchSize(size=10)
    @OrderBy("name")
    private Set<Country> countries;
    @SuppressWarnings("unused")
   @Version
    private Long version;


Country
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@SuppressWarnings("serial")
@BatchSize(size=25)
@NamedQueries({
   @NamedQuery(name=Country.QUERY_ALL_COUNTRY_IDS_AS_SELECT_ITEMS, query="select new javax.faces.model.SelectItem(c.id, c.name) from Country c",
         hints=@QueryHint(name="org.hibernate.cacheable", value="true")),
   @NamedQuery(name=Country.QUERY_ALL_COUNTRIES_AS_SELECT_ITEMS, query="select new javax.faces.model.SelectItem(c, c.name) from Country c",
         hints=@QueryHint(name="org.hibernate.cacheable", value="true"))
})
public class Country implements Serializable {
   public static final String QUERY_ALL_COUNTRY_IDS_AS_SELECT_ITEMS = "allCountryIdsAsSelectItems";
   public static final String QUERY_ALL_COUNTRIES_AS_SELECT_ITEMS = "allCountriesAsSelectItems";

   @Id @GeneratedValue @Column(updatable=false)
   private Long id; //NOPMD - wheelermm
   @Column(unique=true, nullable=false)
    public String name;
   @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
    @ManyToMany(mappedBy="countries", fetch=FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @BatchSize(size=20)
    public Set<Animal> animals;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 5:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you're using the latest version, aren't you?
If thisis the case, can you wrap your code and put it in JIRa, I'll need to have a look

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 7:31 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
I just upgraded to Hibernate core 3.2.2 (released today) and it appears to have fixed the problem. Possibly related to this issue:

HHH-2274

Thanks,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 10:48 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ah, nice.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.