I create an object which I persist to the session. After persisting the object I change the object using a setter method. When the object is being flushed, the old value is inserted into the database.
After digging in the Hibernate source code I have found the following comment in the class AbstractEntityPersister in the method insert(final Serializable id,
final Object[] fields, final boolean[] notNull, final int j, final String sql, final Object object, final SessionImplementor session) throws HibernateException:
Code:
// Write the values of fields onto the prepared statement - we MUST use the state at the time the
// insert was issued (cos of foreign key constraints). Not necessarily the object's current state
This is the case in Hibernate 3.2.4sp1, 3.3.2 and 3.5.5.
I first persist the entity, because in this particular use case I need to show the identifier (which is generated using a sequence when persisting) on the screen.
Why is this (the comment says cos of foreign key constraints, but what can go wrong)? And how can I accomplish that the new values are stored in the database?