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