I'm trying to implement the following association in Hibernate:
I have Users which belong to an Organization via an association class called SecurityRole, which identifies the nature of that relationship (i.e. are they an ADMIN or merely a USER). As an aside, a user may also be a SYSADMIN, which is not associated with an Organization.
So a User has a one-to-many relationship with SecurityRole, as does the Organization. In other words, a User may fulfill multiple SecurityRoles in multiple Organizations. Likewise, an Organization will be associated with multiple Users. This association can be established either by editing the User or the Organization.
The problem I'm having is that I can successfully set up either the User or Organization side of the relationship, but not both at the same time. If I map it to successfully update the SecurityRole via the User, I cannot update it via the Organization. When I try, I get a StaleStateException.
Here are my mappings:
User:
Code:
/**
* @hibernate.set lazy="false" cascade="all-delete-orphan"
* table="security_role"
* inverse="true"
*
* @hibernate.collection-key column="user_key"
*
* @hibernate.collection-one-to-many class="SecurityRole"
*/
public Set getSecurityRoles() {...
Organization:
Code:
/**
* @hibernate.set lazy="false"
* cascade="none"
* table="security_role"
* inverse="true"
*
* @hibernate.collection-key column="organization_key"
*
* @hibernate.collection-one-to-many class="SecurityRole"
*/
public Set getSecurityRoles() {...
SecurityRole:
Code:
/**
* @hibernate.many-to-one class = "User"
* column="user_key"
*/
public User getUser() {...
/**
* @hibernate.many-to-one class =
* "Organization"
* column="organization_key" not-null="false"
*/
public Organization getOrganization() {...
I'm sure this is a simple one. Any help would be greatly appreciated.