Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
J Mehta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic