-->
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: Differnce between load and get (proxy and noproxy)
PostPosted: Wed Nov 19, 2008 1:18 am 
Newbie

Joined: Wed Nov 19, 2008 1:01 am
Posts: 3
Hello,

First time posting so please bear with me. I am just learning hibernate and I am getting a little confused by the difference between 'load' and 'get'. I understand that 'load' returns a proxy while 'get' returns the concrete instance. I can see the debugger variable types. However, why does hibernate execute the complete SQL select during both load and get? If it has already executed the complete SQL why not just always return the concrete instance instead of proxy?

My second question is regarding lazy='proxy' and lazy='noproxy'. From the documentation I see that when a single valued association is related by lazy='proxy' then the only time the object gets loaded is when we access the associated object methods other than the identifier. However, in my debugger I see that for lazy='proxy' when the associated object identifier is accessed the SQL is still run in the database. Why does this happen and what advantage does it give over lazy='noproxy'?

I will really appreciate any response.

Thanks in advance.

Shan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 6:42 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
well using "get" you require him to fetch from database (at least to check the requested row exists), using "load" it may optimize the operation postponing the actual read to when you find out you really need it.
It is possible of course that even when using "load" it isn't possible to avoid the read because of other reasons.

Most probably you tried to read any field (triggering the load) or used a method triggering the read (a toString() for example could be implicitly loaded by your ide when you debug or log something).

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 6:43 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
BTW I didn't try answering your second question because I don't know ;-)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 3:10 pm 
Newbie

Joined: Wed Nov 19, 2008 1:01 am
Posts: 3
(1)OfficeLocation lol = (OfficeLocation) loadById(OfficeLocation.class,
new Long(2));

(2)Long locationId = lol.getLocationId();
(3)System.out.println("Location ID --- " + locationId);
(4)Enterprise lE = lol.getEnterprise();
(5)System.out.println(lE.getEnterpriseId());


Thanks for your response Sanne. To explain what I am doing, look at the code above. I have an Enterprise class which is related in a 'one-to-many' relationship with OfficeLocation class.

After (1) executes I do not see any SQL executing and the 'lol' instance is a javassist proxy. After (2) executes I see the SQL executing, even though it is just the 'id' of the instance I am accessing. Now again after (4) executes I see no SQL but when (5) executes I do see SQL executing.

I was under the impression that hibernate uses proxy so that it doesn't have to go to the database as long as 'id' is accessed. However everytime I see SQL statements executing when 'id' is accessed. This is what I am having difficulty in understanding.

Shan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 3:29 pm 
Newbie

Joined: Wed Nov 19, 2008 1:01 am
Posts: 3
I am following up on this thread to share with the forum my understanding of lazy (might be of use to someone).

(1) I do not believe there is any difference between 'no-proxy' and 'proxy' associations. I did not see any difference in the SQL generated and when they were generated. This is not in accordance with what the reference document mentions in Chapter 19 on hibernate's fetching strategies.

(2) When you use 'load' to get an instance a proxy is returned. This is in contrast to 'get' which right-away returns an instance. The moment you access any field of the instance loaded using 'load' (including the ID) the SQL gets executed and the proxy is loaded with the row from the database. After this you can close the session and continue to invoke any other method on the proxy and you will be fine.


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.