Search
Calendar
May 2018
S M T W T F S
« Sep    
 12345
6789101112
13141516171819
20212223242526
2728293031  
Your widget title
Archives

Posts Tagged ‘BeanUtils’

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.