wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate, User and Address with association table between Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate, User and Address with association table between" Watch "Hibernate, User and Address with association table between" New topic
Author

Hibernate, User and Address with association table between

Avinash Rawana
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
I have a table with users and address with a bi-directional relationship.
One user can have many addresses. My tables are..

User table (id,name,etc)
Address table (id,alias,street,etc)
UserAddress table(userid,addressid) <-- keys to table

Using Criteria, I've retrieved a user object then found all address objects associated with it..but they are in a different order each time.. How can I fix this ?

My hbm.xmls are as follows.



Address ::



Sample lookup code


--------------<br />Avinash A. Ramana<br />NYIT Student<br /><a href="http://www.aramana.org" target="_blank" rel="nofollow">http://www.aramana.org</a><br />avinash@styleempire.com
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Use an order by clause.

I believe the Restrictions class has a method to get a sort object to add to the Criteria object.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Avinash Rawana
Ranch Hand

Joined: Feb 22, 2006
Posts: 31


I tried that, but it is not working. Anytime I try and order-by one of columns in the address table, it seems to not throw errors ..but doesn't show up in my hibernate SQL..

That code generated this :


It seems to not show up.

I'm not sure if Criteria can order the addresses collection if I'm using an association table in the middle..

Any other ideas? Or maybe I coded that Order incorrectly.

[edited to fix formatting - Paul Sturrock]
[ March 15, 2006: Message edited by: Paul Sturrock ]
Avinash Rawana
Ranch Hand

Joined: Feb 22, 2006
Posts: 31


That is the full query. I messed up before. Obviously I'm trying to query and sort by "alias", but it's not selecting it right on the second part.

[ March 15, 2006: Message edited by: Avinash Rawana ]
[ March 15, 2006: Message edited by: Avinash Rawana ]
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
You may try to use a list instead of a set mapping.

regards Sebastian


Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="http://www.laliluna.de" target="_blank" rel="nofollow">www.laliluna.de</a>
Avinash Rawana
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
I'm trying to figure out how to use a List, but am not quite sure how to do it.

Do you have any resources on how this is done?

BTW, is this because in Java, the SET is unordered?



-Avinash R.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate, User and Address with association table between
 
Similar Threads
hibernate mapping problem
Use of set tag in Hibernate mapping file?
Hibernate hql list filter
joins in Hibernate Annotation
Problem in mapping