-->
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.  [ 2 posts ] 
Author Message
 Post subject: @ManyToMany relation table empty (newbie)
PostPosted: Wed Jul 01, 2009 1:01 pm 
Newbie

Joined: Sun Jun 28, 2009 6:27 am
Posts: 2
Hibernate 3

I have two entities User and Role which holds a bidirectional relationship.
Basically I want to create as many roles as I want, without the need to add any user to it, but every time a new user entity has to become persistent it must belong at least to a role.

User.class

...
// many-to-many bidirectional
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "users")
@Column(nullable = false)
private List<Role> roles = new ArrayList<Role>();
....


Role.class

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="USR_ROLE_FK")
private List<User> users = new ArrayList<User>();


I can create new role and users, but the problem is on the USR_ROLE_FK table which is empty, so not consistent relationship between entities.
On the client side, before to pass the new user entity to persist, I first retrieve the detached role entity, then add it to the new user and finally passed to the ejb.
Personally I think I am missing something about the cascading property, but after several attempts and fails I am stuck.

I need your help, at least some resources with practical examples.
Thanks in advance


Top
 Profile  
 
 Post subject: Re: @ManyToMany relation table empty (newbie)
PostPosted: Thu Jul 02, 2009 7:03 am 
Newbie

Joined: Thu May 15, 2008 10:04 am
Posts: 7
Location: uk
For a bidirectional many-to-many you new to set both sides of the relationship, eg newuser.getRoles().add(role) and role.getUsers().add(newuser). It sounds like you're only building one side of the relationship.


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