Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate, User and Address with association table between

 
Avinash Rawana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Avinash Rawana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may try to use a list instead of a set mapping.

regards Sebastian
 
Avinash Rawana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic