Search
Calendar
May 2017
S M T W T F S
« Sep    
 123456
78910111213
14151617181920
21222324252627
28293031  
Your widget title
Archives

PostHeaderIcon 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 ints, floats, 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.

Leave a Reply