Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force update in Hibernate

Tags:

java

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!

like image 805
gubrutz Avatar asked Mar 03 '10 09:03

gubrutz


2 Answers

ok - found it myself. This does the trick:

Session session = (Session)entityManager.getDelegate();  
session.evict(entity);  
session.update(entity);
like image 137
gubrutz Avatar answered Oct 10 '22 01:10

gubrutz


For transients, you can check

if(session.contains(entity)) {
  session.evict(entity);
}
session.update(entity);
like image 34
jirkamat Avatar answered Oct 10 '22 00:10

jirkamat