• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Hibernate, User and Address with association table between  RSS feed

 
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
 
ranger
Sheriff
Posts: 17314
11
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 ]
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!