Reputation: 1136
I am trying to save an object using session.save as below:
beginTransaction();
sessionFactory.getCurrentSession().save(entity);
entity.setName("Entity " + Math.random());
commitTransaction();
The following sql are generated :
1)insert into entity
2)update set name =
Why should it generate separate update sql even when changes are flushed as late as possible ?
Upvotes: 2
Views: 289
Reputation: 28736
It's the expected Hibernate behavior as explained by Gavin (one of the hibernate developers) here:
Expected Hibernate behavior (we have debated whether this is really correct or not!) is that the INSERT statement will insert exactly the data that was set when save() was called. This gives the user a bit finer grained control, especially in a context where there are triggers etc. However, if you are not careful, it could perform badly.
To avoid the INSERT-then-UPDATE, simply fully initialize the object before saving it.
However, if you are unhappy with that (in my case it was really annoying when using extended persistence context): there is a patch for Hibernate 3.5.6_final here to change this behavior.
Upvotes: 4