Error during managed flush org.hibernate.HibernateException: identifier of an instance of com.package.models.className was altered from 1 to 2


This error occurs when you try to modify the primary key value of your Entity Class or Model class. Below are some solutions you can follow to solve this problem.

Solution 1

If you're telling Hibernate to use a generator class (@GeneratedValue), maybe manually setting the value of the primary key is what's causing the exception. So either remove @GeneratedValue from your Entity Class or don't try to set the primary key property of Entity class manually.

Solution 2

If you want to modify the primary key value of your Entity class, you will have to first detach your Entity from session. To detach Entity from session you can use the detach() method of EntityManager interface. For more information on detach method, you can refer Oracle JavaEE API

Solution 3

If you are using xml file for bean configuration, then check what is the type of Primary key field that you have used in XML file and what is the type of Primary key field in Entity or Bean class. If both are different, then change anyone of them, so that both the types of primary key field are same.
For ex. in hbm.xml file if Primary key field (say id) has type of integer and in Entity or Bean class primary key field(say id) has type long.

If all the above solution doesn't work, and you have yourself found some solution please write that below in comment sections please. It would surely help others.

Solution Worked 4 UpvotesUpvote


Solution Didn't Worked 1 DownvotesDownvote



Search Tags