I had the same problems with the Hibernate eventsystem. After some time debugging and testwritings noticed I that the
PreUpdate/PreInsert event is only fired, when you use transactions and the entity which is going to be saved has been changed.
In a small testapplication I have tried out three cases which are the
following:
1) save / update without transaction and changes to entity
2) save / update with transactions without changed to entity
3) save / update with transactions and changes to entity
In my case only the third constellation causes the preupdate / preinsert events.
Here's an extract of my testcode:
Code:
public class SelectForUpdateTest {
// encases an object of org.hibernate.Session
// with some additional logic
public static HibernateConnectionBean dbconnect = null;
public static void main(String[] args) throws Exception {
dbconnect = new HibernateConnectionBean();
dbconnect.setHibernate_CFG_File("/test/hibernate.cfg.xml");
dbconnect.setUsername("me");
dbconnect.setPassword("alsome");
dbconnect.openDatabaseConnection();
// is the connection established ?
SelectForUpdateTest.CheckDatabaseConnection();
SelectForUpdateTest.testManuallyCommit();
SelectForUpdateTest.testWithoutChange();
SelectForUpdateTest.testWithoutCommit();
dbconnect.closeDatabaseConnection();
}
public static void testManuallyCommit() {
// get a bean
MyBean bw = SelectForUpdateTest.getSomeBean();
System.out.println(bw.getName());
Transaction tx = null;
tx = dbconnect.getSession().beginTransaction();
bw.setName(Long.toString(System.currentTimeMillis()));
dbconnect.getSession().saveOrUpdate(bw);
tx.commit();
}
public static void testWithoutCommit() {
// get a bean
MyBean bw = SelectForUpdateTest.getSomeBean();
System.out.println(bw.getName());
bw.setName(Long.toString(System.currentTimeMillis()));
dbconnect.getSession().saveOrUpdate(bw);
}
public static void testWithoutChange() {
// get a bean
MyBean bw = SelectForUpdateTest.getSomeBean();
System.out.println(bw.getName());
Transaction tx = null;
tx = dbconnect.getSession().beginTransaction();
dbconnect.getSession().saveOrUpdate(bw);
tx.commit();
}
public static MyBean getSomeBean() {
// ...........
}
}
Hope that helps.
Benjamin