Posts Tagged ‘BeanUtils’
org.hibernate.HibernateException: identifier of an instance of … was altered from … to 0
Case
Stracktrace
org.hibernate.HibernateException: identifier of an instance of lalou.jonathan.domain.Foo was altered from 183740934 to 0
Sometimes, the error is slightly different: was altered from XXXX to null
Here is a part of Hibernate mapping:
<id name="fooId" column="fooId"> <generator class="seqhilo"> <param name="sequence">JL_Foo_SEQ</param> <param name="max_lo">10</param> </generator> </id>
Here is the Java code:
Foo sourceFoo = FooDAO.findById(xxxx); Foo foo = new Foo(); foo = BeanUtils.copyProperties(sourceFoo); foo.setFooId(null); FooDAO.createFoo(foo);
Explanation
Using new Foo()
instantiates an object of type Foo
, with all its fields initialized at null
(or zero for int
s, float
s, etc.). Writing explicitly foo.setFooId(null)
removes the object from Hibernate current session.
Fix
Don’t set explicitly the fooId
! Leave Hibernate initialize default values, and set handly other values, without using BeanUtils
.