Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » jpa primary key violation when persisting many-to-many columns
jpa primary key violation when persisting many-to-many columns [message #855870] Wed, 25 April 2012 03:08 Go to next message
Xiwen Luo is currently offline Xiwen Luo
Messages: 2
Registered: April 2012
Junior Member
My entities are: the ID of device which is deiveID has many-to-many relationship with the ID of Lib which is rID

my test code is : two new device entities want to set the same new libentity

the problem is : if i use the same entitymanager to persist that 2 new device entities, it will be ok. but if i use 2 different entitymanager instance to persist them ,the error"primary key violation" will come out. I think the entitymanger try to persist the libentity at the second time, which has already been persisted in the first time.


--------------deviceinfo entity ------------------------------------------------

@Entity
@Table(name="deviceInfo")
public class DeviceInfoEntity extends BaseEntity implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long deviceId;
....
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinTable(name = "device_lib", joinColumns = @JoinColumn(name = "deviceInfo_id",
  referencedColumnName="deviceId"), 
  inverseJoinColumns = @JoinColumn(name = "lib_id", referencedColumnName="rId"))
private List<LibEntity> resourceList = null;
......
}

-------------------------lib entity ---------------------------------------------
@Entity
    @Table(name="lib")
    public class LibEntity extends BaseEntity implements Serializable{
           @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       private long rId;

          @ManyToMany(mappedBy = "resourceList", cascade=CascadeType.ALL,
                  fetch=FetchType.LAZY, targetEntity=DeviceInfoEntity.class)
       private List<DeviceInfoEntity> deviceInfolist = null;

           .....
    }


my test code is:
EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("testPU");
          EntityManager em = emFactory.createEntityManager();


    LibEntity libEntity = new LibEntity();


    DeviceInfoEntity dEntity = new DeviceInfoEntity();
    dEntity.setName("dadadada");
    dEntity.setLibEntity(libEntity);


    DeviceInfoEntity dEntity2 = new DeviceInfoEntity();
    dEntity2.setName("dadadadadddddd");
    dEntity2.setLibEntity(libEntity);


    em.getTransaction().begin();
    em.persist(dEntity);
    em.getTransaction().commit();

    em.close();

    EntityManager em2 = emFactory.createEntityManager();
    em2.getTransaction().begin();
    em2.persist(dEntity2);
    em2.getTransaction().commit();


the error is :
Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.LIB(RID)"; SQL statement:
 INSERT INTO lib (RID) VALUES (?) [23505-165]


Thanks
Re: jpa primary key violation when persisting many-to-many columns [message #857380 is a reply to message #855870] Thu, 26 April 2012 09:44 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1016
Registered: July 2009
Senior Member
Hello,

This is the expected behavior, as you are not allowed to call persist on an existing unmanaged entity. Once you persist libEntity in the first EntityManager, it is now managed in that context and gets inserted on commit. When you then call persist in a second context, you are telling that context you want to insert the entity and so are expected to get an exception - either on persist or during the flush/commit.

If you are going to use a second context, you need to read in the libEntity entity using this context and use it in DeviceInfoEntity references. That, or merge the entity instead of using persist - just be sure you have not made changes elsewhere or they will get overwriten unless you use optimistic locking.

Best Regards,
Chris
Previous Topic:Mbean lookup failed in Weblogic 10.3.5 eclipselink 2.1.3.v20110304-r9073
Next Topic:Setting batch size in EclipseLink 1.1.4
Goto Forum:
  


Current Time: Wed Jul 30 07:17:56 EDT 2014

Powered by FUDForum. Page generated in 0.01648 seconds