-->
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.  [ 3 posts ] 
Author Message
 Post subject: Correct understanding of bidirectional manytomany
PostPosted: Sun Aug 06, 2006 1:19 pm 
Newbie

Joined: Sun Aug 06, 2006 11:37 am
Posts: 1
Hi,

Using, JDK 1.5.07
hsqldb_1_8_0_4
hibernate-3.2.CR2.jar
hibernate-annotations-3.2.0.CR1.jar

I have been trying to create a manytomany association between two entities, UserImpl and RoleImpl. A user has many roles and a role has many users.


User is the owner and defines the following

@ManyToMany(
targetEntity = RoleImpl.class)
@JoinTable(
name = "users_roles",
joinColumns = {@JoinColumn(name = "USER_ID")},
inverseJoinColumns = {@JoinColumn(name = "ROLE_ID")}
)
public Set<Role> getRoles()
{
return roles;
}


On the other side i have...

@ManyToMany(
mappedBy = "roles",
targetEntity = UserImpl.class,
)
public Set<User> getUsers()
{
return this.users;
}


Now if i add roles (which are already persisted) to a user and persist the user with getHibernateTemplate().saveOrUpdate(user);

Then i get what expect, which is that a user is created in the USER table and an entry appears in USERS_ROLES mapping the user to its associated roles.

My problem occurs when i try and do exactly the same but the other way around....

I add a user (again already persisted) to a role and then persist the role with getHibernateTemplate().saveOrUpdate(role) but this time no entry appears in USERS_ROLES - which is what i expected.

Am i misunderstanding the concept of bidirectional manytomany or have i got a problem here?


Top
 Profile  
 
 Post subject: Re: Correct understanding of bidirectional manytomany
PostPosted: Sun Aug 06, 2006 4:49 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
ianpackard wrote:
Am i misunderstanding the concept of bidirectional manytomany

Yep.

Read about it in Hibernate Core docs.
- http://www.hibernate.org/hib_docs/v3/re ... irectional

Quote:
The most important addition here is the inverse="true" attribute


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 9:59 am 
Newbie

Joined: Wed Nov 02, 2005 5:20 pm
Posts: 9
I think you need maintain both ends of the relationship in Java code. That means whenever you add roles to a user, you need to add the user object to role objects' users set at the same time. So does adding users to a role.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.