Skip to main content

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 07:08 Go to next message
Xiwen Luo is currently offline Xiwen LuoFriend
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 ------------------------------------------------

public class DeviceInfoEntity extends BaseEntity implements Serializable{

@GeneratedValue(strategy = GenerationType.AUTO)
private long deviceId;
@JoinTable(name = "device_lib", joinColumns = @JoinColumn(name = "deviceInfo_id",
  inverseJoinColumns = @JoinColumn(name = "lib_id", referencedColumnName="rId"))
private List<LibEntity> resourceList = null;

-------------------------lib entity ---------------------------------------------
    public class LibEntity extends BaseEntity implements Serializable{
       @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();

    DeviceInfoEntity dEntity2 = new DeviceInfoEntity();



    EntityManager em2 = emFactory.createEntityManager();

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

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

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,
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 Sep 23 03:50:16 GMT 2020

Powered by FUDForum. Page generated in 0.01258 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top