I am struggling with some simple usage of @PrePost and @PostPersist, the entity is persisted and rows are inserted into my DB, and there are no exceptions, but the methods tagged with PrePersist/PostPersist are simply not called - everything before or after is gently outputted except the message in the PostPersist/PrePersist method. Here is the test method I use:
Code:
@PostPersist
public void onUserPersisted() {
System.out.println("Post-persist");
throw new Error(); //Just to ensure that if output is redirected, I get an error
}
Then I tried another example from java2s, here is the link with the source code:
http://www.java2s.com/Tutorial/Java/035 ... ersist.htmI slightly changed the above example by replacing EntityManagerFactory with SessionFactory and Session, so the modification looks like this:
Code:
package com.cbfa.entity.listeners;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class ListenerJava2sExample {
static SessionFactory sf = new AnnotationConfiguration()
.addAnnotatedClass(Employee.class) // !! (strange thing #2)
.configure().buildSessionFactory();
static Session session = sf.openSession();
public static void main(String[] a) throws Exception {
session.getTransaction().begin();
Employee emp = new Employee();
emp.setId(1);
session.persist(emp);
session.getTransaction().commit();
session.close();
sf.close();
}
}
In addition, I omitted the Helper class, as I already have a working configuration of my own.
I am using JavaSE library, and have added the following to the classpath:
mysql-connector-java-5.1.13-bin.jar
junit-4.4.jar
hibernate-core-3.3.2.GA.jar
dom4j-1.6.1.jar
slf4j-log4j12-1.5.8.jar
slf4j-api-1.5.8.jar
log4j-1.2.14.jar
commons-collections-3.2.1.jar
jta-1.1.jar
hibernate-annotations-3.4.0.GA.jar
hibernate-validator-3.1.0.GA.jar
hibernate-commons-annotations-3.3.0.ga.jar
javassist.jar
ejb3-persistence-1.0.2.GA.jar
Hibernate settings are lets say, copied from a tutorial, nothing special.
Another strange thing, which may be related to this problem is that if I don't add annotated class manually to session factory (see line with comment "!! (strange thing #2)"), I get the "Unkown entity" exception. In the tutorials I have seen so far, there was no need to call "addAnnotatedClass" to ensure that Entity tag is recognized.
I don't know if this may help, but I also noticed that when I tag entities with @org.hibernate.annotations.Entity
(instead of @javax.persistence.Entity), persistence for that entity does not work at all.
Am I doing something really wrong or ... ? Do I have to explicitly enable the usage of callback methods?