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 ------------------------------------------------

@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 13:44 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
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: Fri Apr 26 11:17:28 GMT 2024

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

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

Back to the top