I have the a Role and a Permission object that I modeled with a ManyToMany relationship (role.getPermissions() and permission.getRoles()) The problem I encountered with this bi-directional ManyToMany was that I could only have changes made to one end be persistent, where I wanted to be able to manage this relationship from both ends (Role and Permission)
I decided to go with 2 OneToMany's to solve this problem (does it?)
I created an assocation object "RolePermission" The Role has a OneToMany to RolePermission, and the Permission has a OneToMany to RolePermission.
The Role and the Permission are the owners of the relationship, I did this by not using the "mappedBy" as described in the Soldier/Troop example (in the hiberate annotation reference documentation)
I would really really appeciate it if someone could let me know if this is the way to go on this. I have many other associative relationships of this type and it would be good to know if this is a mistake before I change 100's of lines of code.
Thanks.
-Phillip
Hibernate version:
3.2.1.ga
Mapping documents:
Code:
package org.authsum.om;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "authsum_role_permission")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class RolePermission {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "object_id")
public Long getObjectId() {
return (objectId);
}
/**
* The setter method for this object identifier.
*/
public void setObjectId(Long objectId) {
this.objectId = objectId;
}
private Long objectId;
private Role role = null;
private Permission permission = null;
@ManyToOne
@JoinColumn(name = "permission_fk", insertable = false, updatable = false)
public Permission getPermission() {
return permission;
}
public void setPermission(Permission permission) {
this.permission = permission;
}
@ManyToOne
@JoinColumn(name = "role_fk", insertable = false, updatable = false)
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
package org.authsum.om;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "authsum_role")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "object_id")
public Long getObjectId() {
return (objectId);
}
/**
* The setter method for this object identifier.
*/
public void setObjectId(Long objectId) {
this.objectId = objectId;
}
private Long objectId;
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="role_fk")
public Set<RolePermission> getRolePermissions() {
return rolePermissions;
}
public void setRolePermissions(Set<RolePermission> rolePermissions) {
this.rolePermissions = rolePermissions;
}
private Set<RolePermission> rolePermissions = new HashSet<RolePermission>();
}
package org.authsum.om;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "authsum_permission")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "object_id")
public Long getObjectId() {
return (objectId);
}
/**
* The setter method for this object identifier.
*/
public void setObjectId(Long objectId) {
this.objectId = objectId;
}
private Long objectId;
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="permission_fk")
public Set<RolePermission> getRolePermissions() {
return rolePermissions;
}
public void setRolePermissions(Set<RolePermission> rolePermissions) {
this.rolePermissions = rolePermissions;
}
private Set<RolePermission> rolePermissions = new HashSet<RolePermission>();
}