-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Mapping Many-to-Many but mainly used as 1-to-Many?
PostPosted: Fri Jul 07, 2006 1:42 pm 
Newbie

Joined: Fri Jul 07, 2006 1:30 pm
Posts: 6
Hibernate version: 3.1.1

Suppose I have 3 tables, a many-to-many.
USERS: (UID,UNAME,STATE_ID)
STATES: (STATE_ID,SNAME)
USER_STATES: (UID,STATE_ID)

My User object HAS a list/set of states, but my States object DOES NOT have a list/set of users (as i dont think that's usually queried that way). So how should I map this case? Its not many-to-many on the object side because States has no list of users. Can someone show me a mapping for this case? Or is the best practice to add a list of Users to the State object? If I was using Ibatis, I'd just map what I need, but I'm sorry if this is a simple question, its just that Hibernate has its own way of working that's not obvious to a new user. Thanks very much help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 2:34 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
Is this what you're looking for?

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-unidirectional-join-m2m


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 3:50 pm 
Newbie

Joined: Fri Jul 07, 2006 1:30 pm
Posts: 6
What happens if I needed later a query by each state all users in each one. Then would I have to add a list of Users to the State object?

And if that was the case, how would Hibernate know how far to stop. For instance user has a list of States, but each State has a list of Users, and each User has a list of States. How does Hibernate know how far to recursively get the object graph?

It could go on infinitely getting sublists, etc?

But thanks, that does look like what I wanted!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 4:12 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
If you want to navigate from State to User, you would indeed need to make it bi-directional. But lazy fetching should prevent the recursive nightmare you talk about (the object graph is fetched only as deeply as you access it). Reading the documentation, it seems there is even an option for extra-lazy fetching (lazy="extra"), although I've never used this myself.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.