-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: AuditLogging, cannot configure listeners in persistence.xml
PostPosted: Tue Jun 24, 2008 6:30 am 
Newbie

Joined: Tue Jun 24, 2008 6:06 am
Posts: 5
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?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 8:31 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi,

I also do audit logging in my application, but I do not build on events.
I've the same configuration (JPA / EJB3) and I created an interceptor class and registered the interceptor in the file "../META-INF/persistence.xml" like this:
Code:
<!-- Interceptor for audit logging -->
<property name="hibernate.ejb.interceptor" value="org.dpjw.history.server.HistoryInterceptor" />


The interceptor is notified by Hibernate on this method
Code:
public boolean onFlushDirty(final Object pEntity,
                              final Serializable pId,
                              final Object[] pCurrentState,
                              final Object[] pPreviousState,
                              final String[] pPropertyNames,
                              final Type[] pTypes) throws CallbackException {

, which I've implemened.

Maybe this helps you.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 9:24 am 
Newbie

Joined: Tue Jun 24, 2008 6:06 am
Posts: 5
Hi CarlŠ¾,
And the question is, how do you get a connection to the database (session, EM, whatever) in this method?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 1:40 pm 
Newbie

Joined: Tue Oct 21, 2008 11:21 am
Posts: 2
Hi Mihail,

Did you get an answer to this?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 8:44 am 
Newbie

Joined: Tue Jun 24, 2008 6:06 am
Posts: 5
No, I didn't, I'm sorry.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.