5

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")
3
  • See: stackoverflow.com/questions/734461/… Commented Aug 5, 2011 at 7:00
  • 1
    Something wrong with your database design if you need to change the primary key. Please tell me that you don't use "mark" as primary key. Quick answer : Just do a copy of the record, save it as another record with different id, delete the older record. Commented Aug 5, 2011 at 7:02
  • As an afterthough after rambling - Did you mean change the primary key columns or the object's value of the primary key? Commented Aug 5, 2011 at 7:03

2 Answers 2

15

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.
Sign up to request clarification or add additional context in comments.

1 Comment

+1 and I agree with this approach. It seems that the poster put name of a person as primary key.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.