This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate sort collection having many-to-many relationship with order-by attribute
 
Similar Threads
Inserting data in Intermediate table on the Many-to-Many
Use of set tag in Hibernate mapping file for foreign key
simple example implementation?
hibernate 1-many insertion
Hibernate - Foreign Keys