First, what's my goal
I want to do an audit log. I read the
http://www.hibernate.org/318.html and there two different approaches were mentioned.
The first is using a Listener.
The second is using the new eventing model in Hibernate3.
The author said that he couldn't make the eventing approach work for an unknown reason.
I'm using Hibernate via JPA. I'm using it in an EJB. So the problem with the first approach is that it is difficult (if possilbe) to supply the same transaction to the interceptor so that it can write an entry in the audit log. There is a solution with a static field holding a session or an EM, but that seems ugly.
So I chose the second approach. I created a class, inherited few eventhandlers (PreInsertEventListener, PostUpdateEventListener ) and tried to add the class as a listener.
Using the API, it worked (the callback methods in the listeners were called):
Code:
AnnotationConfiguration configuration = new AnnotationConfiguration();
[b]
configuration.setListener( "post-update", new Test1() );
[/b]
configuration.addProperties( hibernateProperties );
EntityManagerFactory factory = new EntityManagerFactoryImpl( configuration.buildSessionFactory(),
PersistenceUnitTransactionType.RESOURCE_LOCAL, true );
EntityManager result = factory.createEntityManager();
Using properties (as if using persistence.xml) failed(the callback methods in the listeners were
NOT called):
Code:
Properties hibernateProperties = new Properties();
...
[b]
hibernateProperties.put( "hibernate.ejb.event.pre-update", "package.Test1" );
hibernateProperties.put( "hibernate.ejb.event.pre-insert", "package.Test1" );
[/b]
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.addProperties( hibernateProperties );
EntityManagerFactory factory = new EntityManagerFactoryImpl( configuration.buildSessionFactory(),
PersistenceUnitTransactionType.RESOURCE_LOCAL, true );
EntityManager result = factory.createEntityManager();
Ideas why?