The following entities are defined in my system:
Code:
@Entity
@Table(name="roles")
public class Role
{
@Id
@GeneratedValue
@Column(name="ROLE_ID")
private Long id;
@OneToMany(
fetch=FetchType.LAZY,
cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},
mappedBy="role"
)
private List<RoleFunctionAccess> roleFunctionAccess;
// other stuff
};
@Entity
@Table(name="role_function_access")
public class RoleFunctionAccess
{
@SuppressWarnings("serial")
@Embeddable
public static class Id implements Serializable
{
// Composite Id Variables
@Column(name = "ROLE_ID")
private Long roleId;
@Column(name = "FUNCTION_ID")
private Long functionId;
// other stuff
}
// Variables
@EmbeddedId
private Id id = new Id();
@Column(name = "ACCESS_FLAGS")
private Long accessFlags;
@ManyToOne
@JoinColumn(name="ROLE_ID",insertable=false,updatable=false)
private Role role;
// other stuff
};
When I edit a role, I do rolesService.findById() to get the unique Role entity from the datastore and I pass this to my page for rendering. The problem I face first of all is that I need to place some constraints of the RoleFunctionAccess entity in that if the accessFlags gets set to 0 the record should be deleted, not updated. Additionally, if the value is NULL, it should not be inserted into the database.
These types of constraints, do I have to handle this on my own or is there a way to do this within Hibernate?