Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update primary key in java Hibernate

Tags:

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")
like image 600
bojaraj G.K Avatar asked Aug 05 '11 06:08

bojaraj G.K


1 Answers

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.
like image 128
Aleksi Yrttiaho Avatar answered Oct 04 '22 17:10

Aleksi Yrttiaho