-->
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: Interceptor and lazy loading question
PostPosted: Wed Mar 02, 2005 7:13 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
I have a Hibernate2 application that uses the Interceptor interface to track changes to data in an audit trail. (The audit trail records both the before and after value of an object for each audit event.)

The javadoc for the Interceptor interface makes it clear that "the session may not be invoked from a callback (nor may a callback cause a collection or proxy to be lazily initialized)."

I've been able to avoid this situation because
1) A seperate Hibernate session is used to save the audit objects to the database.
2) All collections that are audited in this app are mapped as collections of entities instead of collections of values (the entities in these collections have there own lifecycle and so they will each get there turn in the Interceptor). As I iterate over the properties of an entity in the interceptor I can skip over any where isPersistentCollectionType() is true.
3) All proxied objects in this particular app have, by circumstance, not required auditing .... that is until now.

If I were to reference an uninitialized before value of a proxied object in the interceptor, I would be violating the restriction specified in the javadoc.

So, now to my question:
Is there a way to obtain the uninitialized previous value of a proxied object (or lazily loaded collection for that matter) in an Interceptor callback without violating this restriction? Has anyone encountered this same problem and found a solution?

Does the event interface framework in Hibernate3 provide a different way to do this?

Regards,
-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 11:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
An uninitialized proxy, by definition, cannot be modified in terms of its persistent state. Why would you need to audit it?

Quote:
Is there a way to obtain the uninitialized previous value of a proxied object

No idea what you are asking here.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 11:38 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
steve wrote:
An uninitialized proxy, by definition, cannot be modified in terms of its persistent state. Why would you need to audit it?


If uninitialized proxied object is updated without first referencing the current value, the previous value that Hibernate saved off and provides to the Interceptor as the previous value will be uninitialized.

Consider:

Code:
<hibernate-mapping>
   <class name="Foo" table="Foo">
      ...
      <many-to-one name="status" class="Status" cacade="all"/>
      ...
   </class>
<hibernate-mapping>

<hibernate-mapping>
   <class name="Status" table="Status" lazy="true">
      ...
   </class>
</hibernate-mapping>


Then somewhere in application code:

Code:
...
Foo f = session.find(...);

// note f.status is unintialized at this point --
// and is not referenced in this code

f.setStatus(new Status(...));

f.save();




Now in the interceptor, if I try reference the previous (uninitialized) value of status in this foo object, it will cause it to be initialized which, in an interceptor callback, is not allowed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 10:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The proxy for Status is not initialized until you access a property of the Status class itself (other than the id getter, and potentially equals/hashCode).

So I am not really at all understanding the issue here.

Initially you have a proxy for Status(id=1), later you overwrite that with a newly instantiated Status. Why in the world would you need to compare the *state* of the original proxy with the new instance?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 12:40 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
steve wrote:
The proxy for Status is not initialized until you access a property of the Status class itself (other than the id getter, and potentially equals/hashCode).

So I am not really at all understanding the issue here.

Initially you have a proxy for Status(id=1), later you overwrite that with a newly instantiated Status. Why in the world would you need to compare the *state* of the original proxy with the new instance?


I would like to obtain the previous value to record in the audit trail. If this property was not referenced before it was set the previous value is not initialized. Referencing it in the Interceptor callback (in the previousState array) would then cause it to be initialized which the Interceptor's javadoc says is prohibited.

I guess I'm not explaining this very well. What it boils down to is that I have a need to obtain the previous value of a uninitialized proxy in an Interceptor callback. Is there a way to do this that doesn't violate the restriction dictated in the Interceptor javadoc?

Interestingly enough, in my contrived sample code the referencing of this uninitialized proxy in the Interceptor seems to work. Maybe I don't understand the restriction in the javadoc?


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.