-->
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.  [ 4 posts ] 
Author Message
 Post subject: event listener usage
PostPosted: Thu Dec 08, 2005 1:13 am 
Newbie

Joined: Sat Jul 24, 2004 1:53 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hi

Hibernate version: 3.05

i've just started to use the event listener classes in hibernate3 and am a little confused as to their intended use.

what i am trying to achieve is to trigger an additional update to the one being performed by the system. basically - if record type x is being updated, update record y also. i have tried using preinsert, preupdate and onflushentity events but the second update is not happening. i have added debug statements that show me that the listeners are being triggered after a commit is called and before the commit returns.

some sample code:
Code:
    public void onFlushEntity(FlushEntityEvent event) {
        Object entity = event.getEntity();
       
        Log.debug("Listener: onFlushEntity for entity " +
                        entity.getClass().getName());

        Session session = event.getSession();
        if (entity.getClass() == MyObject.class) {
            MyObject myObject = (MyObject)entity;
            Integer secondObjectId = myObject.getSecondObject().getObjectId();
            SecondObject secondObject = (SecondObject)session.load(SecondObject.class, secondObjectId);
           
            secondObject.setSomeBooleanValue(new Boolean(false));
            session.update(secondObject);
        }


i have tried session.flush() both here and in the underlying tx mechanism that handles the tx control and closes the session at the end. no change.
when session.flush() is called i can see that the listeners are triggered again and so forth but the second object's record is not being updated.

do i have it all wrong - is there no intention to use the event listeners for further entity changes to be persisted?

any suggestions would be appreciated.

Takis


Top
 Profile  
 
 Post subject: ==
PostPosted: Thu Dec 08, 2005 8:17 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Quote:
entity.getClass() == MyObject.class


That prevents code from working.
As a rule of thumb: do not use == on objects.

For your code I suggest using
If( o instanceOf MyObject ){

}

Reason: at runtime H enhances persistent classes and therefore actual type of o is the subclass of MyObject class

Or use if ( “MyObjectClassName”.equals(event.getEntityClassName()) )

http://www.hibernate.org/hib_docs/v3/re ... vents.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 12:05 am 
Newbie

Joined: Sat Jul 24, 2004 1:53 am
Posts: 4
Hi

thanks for responding.

the reason i use it that way in this case is that there is in fact a number of entity types i'm checking against - its eaiser to loop through an array of class objects than a series of if instanceof else if etc.... object comparison in this case is not i believe an issue - indeed i've never had a problem.

someone may want to correct me if wrong here but according to the java spec there exists only one class object instance (as in MyObject.class or myObject.getClass()) of any particular object within a jvm so comparison of myObject.getClass and the MyObject.class will work regardless of how many MyObjects objects you have and wish to make a comparison or otherwise against.

in any case. this is not quite the issue i am having here. the use of the event framework doesn't seem to easily support what i'm trying to achieve in my original post (???).

any further suggestions would be appreciated.
thanks.

Takis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 5:23 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
takisd wrote:
Hi


someone may want to correct me if wrong here but according to the java spec there exists only one class object instance (as in MyObject.class or myObject.getClass()) of any particular object within a jvm so comparison of myObject.getClass and the MyObject.class will work regardless of how many MyObjects objects you have and wish to make a comparison or otherwise against.

Takis


Very very outdated and hence wrong assumption. There might be any number of class objects for the same class within a JVM.
There likely to be one class definition per classloader hierarchy, but might be one class definition per classloader, even multiple class definition per classloader.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.