File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate sort collection having many-to-many relationship with order-by attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate sort collection having many-to-many relationship with order-by attribute" Watch "Hibernate sort collection having many-to-many relationship with order-by attribute" New topic
Author

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

J Mehta
Greenhorn

Joined: Mar 29, 2013
Posts: 2
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

Joined: Apr 04, 2006
Posts: 108

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


Thanks,
Tushar (SCJP 1.5)
J Mehta
Greenhorn

Joined: Mar 29, 2013
Posts: 2
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.
 
GeeCON Prague 2014
 
subject: Hibernate sort collection having many-to-many relationship with order-by attribute