• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate sort collection having many-to-many relationship with order-by attribute

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have three tables.
Employee
Employee_Site
Site

Employee have employee related information including EmpID.
Site have Site related information including site id and site name.

Employee_Site have EmpID and SiteID records.
So, here we have multiple SiteIDs for given EmpID.

Also, we have mapping files for 3 classes.

Employee class have "sites" attribute with type as Set
class Employee {
----
---
Set sites;
}
Employee hbm file has entry as below-
<set name="sites" table="Employee_Site">
<key column="EMP_ID" foreign-key="FK_EMPLOYEE_ID" />
<many-to-many column="SITE_ID" class="Site" foreign-key="FK_SITE_ID" />
</set>

Now, when employee gets loaded I wanted to have set of Site as sorted. Sorted by site name, that is stored in Site table.
And, I wanted to use order-by clause. Can anyone tell me how I can achieve this ?

Thanks
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

This is how I can think of -
1. Site implements Comparator. Compare by site name.
2. Association - Emp -> TreeSet<Sites>
3. Overide Intercepor -> onLoad() for emp, Use criteria with order by clause to load sites
 
J Mehta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion to use interceptors.

So, I believe there is no straight forward way to achieve the sorting using order by clause in scenarios like this.

Thanks.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic