Hi All,
I have two tables: ROLE and PRIVILEGE and relationship between those is MANY-TO-MANY. This is why I have created a mapping table ROLE_PRIV. Pojo Configurations are shown below:
DemoRole.javaCode:
@Entity
@Table(name="demo_role")
@Access(AccessType.FIELD)
public class DemoRole implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -8956577999485943955L;
@Id
@Column(name="ID")
private Long id;
@Column(name="NAME")
private String name;
@ManyToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable
(
name="demo_role_priv",
joinColumns={@JoinColumn(name="ROLE_ID", referencedColumnName="ID")},
inverseJoinColumns={@JoinColumn(name="PRIV_ID", referencedColumnName="ID")}
)
private Set<DemoPrivilege> privileges;
// -- Getters/Setters
DemoPrivilegeCode:
@Entity
@Table(name="demo_privilege")
@Access(AccessType.FIELD)
public class DemoPrivilege implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 4790886390281031561L;
@Id
@Column(name="ID")
private Long id;
@Column(name="NAME")
private String name;
// -- Getters/Setters
I did not put any ManyToMany relationship in DemoPrivlege class because the relationship is owned by DemoRole. Privileges are pre-existing.
I am using following code to update the user-role association.
Code:
final DemoRole mergedEntity = entityManager.merge(role);
entityManager.persist(role);
entityManager.flush();
Although this is updating the association, but also, it is updating the mater records ( from DemoRole and DemoPrivilege classes).
Could someone help me to understand what mistake I am doing?
Thank,
Niranjan