-->
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: Problem controlling state changes
PostPosted: Tue Jun 13, 2006 12:11 pm 
Newbie

Joined: Wed May 17, 2006 11:48 am
Posts: 5
I need to create an automatic change history support for hibernate entities. I started in a conventional way. Created a class that extends EmptyInterceptor and by overriding onDelete, onFlushDirty and onSave I was able to capture and persist the entity state changes.

My problem is that I also make state changes in the entities as they are disconnected of the session and in this case the previousState parameter of the onFlushDirty callback method does not reflect the changes made. To fix I generated code to register all set property methods called but the hibernate framework also uses the set methods to load the beans and I am not able to know the difference between the framework calls from the user calls.

At this point, I am convinced that I need to change the Interceptor interface and supporting methods to create an afterLoad method so that I could only consider as state changes the set property methods called after entity loading is complete.

I would appreciate any ideas on how to fix this problem in a way that spare of making changes in the Hibernate framework.

Marco


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 10:53 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There's a postLoad event listener, you could switch to using that, if you need it.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 9:52 am 
Newbie

Joined: Wed May 17, 2006 11:48 am
Posts: 5
I thank you very much for your suggestion. I tried it in a stand-alone application and it worked just fine. In this test, I set the listener using the setListener method in the Configuration interface. However, I have to deploy the application in a JBoss environment automatically configured using a hibernate-service.xml file. As it only exposes (through JNDI lookup) the resulting session factory (that does not keep a reference to the original configuration) I was not able to set the listener in the same way.

I have got it working by changing the listeners of my current session as shown below:

Code:
Session s = sessionFactory.getCurrentSession();
SessionImpl sImpl = (SessionImpl) s;
EventListeners listeners =  sImpl.getListeners();
listeners.setPostLoadEventListeners(new PostLoadEventListener[]{this});


I still wonder if there is a more canonical way to set the posLoad listener in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 5:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes, I have a very similar problem in one of my apps, and have to cast to SessionImpl to get around it too. If you cannot do it where the session factory is created, and if you cannot set up your listeners in your .cfg.xml file, then this is the way to go. Are you not allowed edit your .cfg.xml file? Have a look in the ref docs section 12.2, "Event system": it shows you how to set up listeners, including post load ones, in the configuration file. If that route is open to you, you won't have to cast to an impl class.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 12:13 pm 
Newbie

Joined: Wed May 17, 2006 11:48 am
Posts: 5
As I am using the Hibernate MBean to create declaratively the configuration and the session factory, I do not use a .cfg.xml file. But, starting from your suggestion I looked up the Hibernate MBean javadocs and found two configuration properties (SessionFactoryInterceptor and ListenerInjector) that allowed me to set the proper event listeners also declaratively in the hibernate-service.xml file. It now not only works but also is nicely integrated with the Hibernate framework. Once more, I thank you for your help.

Marco


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.