Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@ManyToMany relation table empty (newbie)

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic