gubrutz
gubrutz

Reputation: 259

Force update in Hibernate

How can I force Hibernate to update an entity instance even if the entity is not dirty? I'm using Hibernate 3.3.2 GA, Hibernate Annotations and Hibernate EntityManager btw. I really want Hibernate to execute the generic UPDATE statement even if no property on the entity has changed.

I need this because some event listeners need to get invoked to do some additional work when the application runs for the first time.

Thanks!

Upvotes: 11

Views: 14922

Answers (4)

jirkamat
jirkamat

Reputation: 1788

For transients, you can check

if(session.contains(entity)) {
  session.evict(entity);
}
session.update(entity);

Upvotes: 4

Lukasz Frankowski
Lukasz Frankowski

Reputation: 3175

session.evict(entity);  
session.update(entity);

Good trick, but watch out for transient objects before putting this into some automation code. For transients I have then StaleStateObjectException

Upvotes: 2

Sujal Shelke
Sujal Shelke

Reputation: 1

Try em.flush() which is used for EJB 3.0 entities, which also uses JPA similar to Hibernate 3.2.2 GA. If it doesn't work normally, use flush in transactions.

Upvotes: 0

gubrutz
gubrutz

Reputation: 259

ok - found it myself. This does the trick:

Session session = (Session)entityManager.getDelegate();  
session.evict(entity);  
session.update(entity);

Upvotes: 14

Related Questions