-->
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.  [ 9 posts ] 
Author Message
 Post subject: Fundamental question regarding Transitive Persistence
PostPosted: Tue Aug 16, 2005 8:40 am 
Newbie

Joined: Tue Aug 16, 2005 8:28 am
Posts: 15
Say I have a Person object that has an Address object. Now this is a one-one association. Now I set up the association to have cascade = "all"

I retrieve a Person from the database in session1

Then I do

Address a = person.getAddress();
a.setAddressLine1("line 1");

Then I do a session.save(person);

As per transitive persistence when the Person is saved, the Address should also get saved.

My question is: How did Hibernate know that the address object had the line1 modified?

How does Hibernate detect this in one-one associations? Does it use Proxies? Or does it use a brute-force method to check all one-one associations?


Top
 Profile  
 
 Post subject: Re: Fundamental question regarding Transitive Persistence
PostPosted: Tue Aug 16, 2005 11:20 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
pearl8 wrote:
My question is: How did Hibernate know that the address object had the line1 modified?

How does Hibernate detect this in one-one associations? Does it use Proxies? Or does it use a brute-force method to check all one-one associations?


Any operations in Hibernate on People object would cascade to associations, declared with appropriate cascading type. If you declare in People association to Address with cascading type "all", then all operations would cascade.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 11:33 am 
Newbie

Joined: Tue Aug 16, 2005 8:28 am
Posts: 15
My question is: How did Hibernate know that the address object had the line1 modified?

How does Hibernate detect this in one-one associations? Does it use Proxies? Or does it use a brute-force method to check all one-one associations?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 12:45 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
pearl8 wrote:
My question is: How did Hibernate know that the address object had the line1 modified?

How does Hibernate detect this in one-one associations? Does it use Proxies? Or does it use a brute-force method to check all one-one associations?


It's depend on your code. If object in question is not associated with the session at the time when the flushing occured, then dirty checking is not performed. If dirty checking is performed, then it cascades to associations with cascading style "save-update" or "all"

Particular operations (save/update etc.) usualy do not perform dirty checking of objects - they simple do cascading to appropriate associations.

Method, used for dirty checking is depends on your settings - usually this is snapshot comparision for values of properties, and special proxy implementations for collections.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 10:47 am 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
If you load an object from the database, it's very likely to be proxied. When you save an object that has cascades, it checks the proxied collections to see if anything has been added/removed, then checks the contained objects to see if they, or any of their cacades, are dirty.

So, the short answer for loaded objects is proxies. One of the common mistakes in hibernate-using code is to set a new collection on a loaded object, rather than to use clear().

For new objects, it pretty obviously just traverses the object graph, unless it encounters a proxied object.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 11:43 am 
Newbie

Joined: Tue Aug 16, 2005 8:28 am
Posts: 15
in this case i'm not using collections.

person.getAddress() returns a strongly typed Address object. so do you think there would be a proxy Address object that would be created for such an association?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 12:28 pm 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
Assuming that Person was loaded by Hibernate from the database, and that Address is a mapped entity, where the mapping indicated a -to-one relation from Person to Address, then yes, it'll be a proxy.

Unless, of course, you've set the hibernate configuration variable to stop cglib from being used (if you don't know what I'm talking about, you haven't changed it). :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 4:20 am 
Newbie

Joined: Tue Aug 16, 2005 8:28 am
Posts: 15
well apparently it doesn't turn out to be a proxy till such time you specify proxy=true in Address.hbm.xml

I further validated this be deliberately creating an ArithmeticException after fetching the Address to check out the stack trace. In case of

proxy=true I could see this proxying

[java] Exception in thread "main" java.lang.ArithmeticException: / by zero
[java] at com.oreilly.hh.Address.setName(Address.java:44)
[java] at com.oreilly.hh.Address$$FastClassByCGLIB$$a0d0e20f.invoke(<generated>)
[java] at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
[java] at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108)
[java] at com.oreilly.hh.Address$$EnhancerByCGLIB$$77f9cf7f.setName(<generated>)
[java] at com.oreilly.hh.QueryTest3.main(QueryTest3.java:40)

It wasn't the case in case of proxy=false


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 5:28 am 
Newbie

Joined: Tue Aug 16, 2005 8:28 am
Posts: 15
my bad - it should read till you specify lazy=true in Address.hbm.xml and not proxy=true


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