aspose file tools*
The moose likes Object Relational Mapping and the fly likes @ManyToMany relation table empty (newbie) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "@ManyToMany relation table empty (newbie)" Watch "@ManyToMany relation table empty (newbie)" New topic
Author

@ManyToMany relation table empty (newbie)

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
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


trying to decode a woman mind....
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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.


Hmmm.....I'm not sure how you'll enforce this at the JPA level. I'm not saying you can't, I'm just saying that in my limited knowledge, I don't know how I'd do it.

What about doing it at the Java level?

So, use has a constructor that takes a role. It has only one constructor, so it can't be created without a role:



Now, if the role is null, you throw an exception:



Is it feasible?

Not sure if this fits your need. Perhaps it's at least a talking point.

-Cameron McKenzie
Lalit Bhatt
Ranch Hand

Joined: Dec 27, 2007
Posts: 69

An observation:

- You have put mappedBy on User side, that means the role side is the owning side. I think it's better to make the User side as the owning side. Because the roles are attached or detached to users and not the other way round.


Java-JavaEE Hibernate Spring Software Development Business Process Analysis
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Move the owner doesn't make any difference, I think that in a biderectional relationship I have to manage the relation by myself.
Something like this, before to persist new entities.

Role existRole = (Role)em.find(Role.class, id);
em.persist(newUsr);
newUsr.getRoles().add(existRole);
existRole.getUsers().add(newUsr);

//it shouldn't be necessary, but...
em.merge(existRole);
em.merge(newUser);

And at this point I'd add the Transaction required annotation as well

Not sure it works, but apparently let Hibernate to manager bidirectional relationship doesn't work as expected.
Thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @ManyToMany relation table empty (newbie)