-->
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.  [ 3 posts ] 
Author Message
 Post subject: Proxies and database hit
PostPosted: Sun Nov 02, 2008 1:13 am 
Newbie

Joined: Tue May 27, 2008 7:11 am
Posts: 5
Hello,

I have a general understanding of Hibernate but I'm more interested in cashing capabilities, so I've started from Ch.13. But the following example confused me a little bit:

ch13, p565:
Quote:
A proxy is useful if you need the Item only to create a reference, for example:
Code:
   Item item = (Item) session.load(Item.class, new Long(123));
   User user = (User) session.load(User.class, new Long(1234));
   Bid newBid = new Bid("99.99");
   newBid.setItem(item);
   newBid.setBidder(user);
   session.save(newBid);

You first load two objects, an Item and a User. Hibernate doesn’t hit the database
to do this: It returns two proxies. This is all you need, because you only require
the Item and User to create a new Bid. The save(newBid) call executes an INSERT
statement to save the row in the BID table with the foreign key value of an Item
and a User—this is all the proxies can and have to provide. The previous code
snippet doesn’t execute any SELECT!


As far as I understand, session.load is the Hibernate way to retrieve by primary key. If the previous snippet doesn't execute any SELECT, how Hibernate knows that an Item entity with id 123 exists?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 02, 2008 2:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It doesn't. It just generates an instance of Item with that identifier value. We all hope that it really exists in the database as a row. If it doesn't, you will get an exception at some point, in the code example as soon as you try to save the Bid instance. The foreign key reference to the item identifier does then not resolve.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 02, 2008 2:57 am 
Newbie

Joined: Tue May 27, 2008 7:11 am
Posts: 5
Thanks.


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