Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Not able to create entity after changing the primary key(JPA does not allow changing primary key for it's own reason.I am tyring to work it around by first removing the entity and then creating it back with changed primary key. However, when I try to pers)
Not able to create entity after changing the primary key [message #1058762] Tue, 14 May 2013 19:37 Go to next message
Deepak Somaiya is currently offline Deepak Somaiya
Messages: 4
Registered: September 2011
Junior Member
Here is my sample program

================
Entity myentity = entityManager.find(class, key);

entityManager.remove(myentity);

entityManager.flush();

myEntity.setId(xyz) // id is primary key

entityManager.persist(myentity);
entityManager.flush();

========

I even tried, detaching entity (as well entityManager.clear()) but that did not help either, so basically i tried this too



Entity myentity = entityManager.find(class, key);

entityManager.remove(myentity);

entityManager.flush();

entityManager.detach(myentity);

myEntity.setId(xyz) // id is primary key

entityManager.persist(myentity);
entityManager.flush();



I am using eclipse link 2.3.2. can someone please tell me how to make this work?

Deepak
Re: Not able to create entity after changing the primary key [message #1058789 is a reply to message #1058762] Wed, 15 May 2013 02:34 Go to previous messageGo to next message
Deepak Somaiya is currently offline Deepak Somaiya
Messages: 4
Registered: September 2011
Junior Member
Just want to add that I am getting following error when I am trying to persist the entity (one removed) with different primary key

The attribute [xxxxx] of class [MyEntity] is mapped to a primary key column in the database. Updates are not allowed.
Re: Not able to create entity after changing the primary key [message #1060016 is a reply to message #1058789] Wed, 22 May 2013 10:30 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You cannot change an object's id. Instead create a new object, or clone/copy the object.

James : Wiki : Book : Blog : Twitter
Previous Topic:Convert varchar to nvarchar for specific columns only.
Next Topic:NPE in DatabaseQueryMechanism.updateObjectForWriteWithChangeSet(DatabaseQueryMechanism.java:978)
Goto Forum:
  


Current Time: Thu Jul 24 14:01:58 EDT 2014

Powered by FUDForum. Page generated in 0.01543 seconds