Mihail Stoynov's blog!
Surrender your ego
Tuesday, June 24, 2008
Hibernate, AuditLogging, cannot configure listeners in persistence.xml
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):
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.setListener( "post-update", new Test1() );
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):
Properties hibernateProperties = new Properties();
...
hibernateProperties.put( "hibernate.ejb.event.pre-update", "package.Test1" );
hibernateProperties.put( "hibernate.ejb.event.pre-insert", "package.Test1" );
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.addProperties( hibernateProperties );
EntityManagerFactory factory = new EntityManagerFactoryImpl( configuration.buildSessionFactory(),
PersistenceUnitTransactionType.RESOURCE_LOCAL, true );
EntityManager result = factory.createEntityManager();
Ideas why?
Comments [2]
|
Trackback
Wednesday, September 24, 2008 7:49:53 AM UTC
hibernate.ejb.event.<eventtype> Event listener list for a given eventtype.
The list of event listeners is a comma separated fully qualified class name list (eg. hibernate.ejb.event.pre-load com.acme.SecurityListener, com.acme.AuditListener)
kanwar
Thursday, September 25, 2008 11:01:31 AM UTC
Am I not doing exactly that?
mihail.stoynov
OpenID
Please login with either your
OpenID
above, or your details below.
Name
E-mail
(will show your
gravatar
icon)
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, blockquote@cite, strike
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
dasBlog theme by
Mads Kristensen
RSS feed
Search
Archives
June, 2009 (13)
May, 2009 (8)
April, 2009 (1)
March, 2009 (7)
February, 2009 (2)
January, 2009 (1)
December, 2008 (2)
November, 2008 (15)
October, 2008 (16)
September, 2008 (30)
August, 2008 (15)
July, 2008 (14)
June, 2008 (26)
May, 2008 (6)
April, 2008 (21)
March, 2008 (14)
February, 2008 (28)
November, 2007 (5)
October, 2007 (7)
September, 2007 (1)
August, 2007 (7)
July, 2007 (3)
June, 2007 (1)
Blog Stats
Total Posts: 242
This Year: 37
This Month: 0
This Week: 0
Comments: 167
Categories
Did you know
Java
rulez
Sucks
The USA trip
БГ
Blogroll
Michael Moore (no rss)
Links
BG-JUG
Copyright policy
No rights reserved.
(You are going to
copy stuff anyway :)
If you mention my
name, thank you.
2009, Mihail Stoynov