bojaraj G.K
bojaraj G.K

Reputation: 51

how to update primary key in java Hibernate

I am working on Mvc architecture,I have pojo object i want change primary key please can you help me Primary key is varchar Ex:

Users users=usersDao.load("mark")

Upvotes: 5

Views: 15986

Answers (2)

Bogdan
Bogdan

Reputation: 713

as still as I understand you need to try SQL/JDBC directly. And then somehow try to invalidate the entity from hibernate inner state and all caches.

Upvotes: 0

Aleksi Yrttiaho
Aleksi Yrttiaho

Reputation: 8446

You should never change the value of the primary key. The primary key defines the identity of the object. Hibernate or any other JPA implementation cannot identify the object as the same object if the primary key has been changed. If the primary key is subject to change, you should either choose another primary key or generate a surrogate key.

The workaround is to

  1. Delete the existing object
  2. Persist the object again with a different primary key value.

Upvotes: 15

Related Questions