-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: JPA Update - Updating records in the parent tables
PostPosted: Mon May 28, 2012 6:04 pm 
Newbie

Joined: Sun May 06, 2012 12:47 am
Posts: 3
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.java
Code:
@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


DemoPrivilege
Code:
@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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.