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.  [ 2 posts ] 
Author Message
 Post subject: How to read id of a proxy without triggering a lazy load?
PostPosted: Wed Dec 03, 2008 1:21 pm 
Newbie

Joined: Wed Dec 03, 2008 11:23 am
Posts: 2
I'm using nHibernate 2.0 with C#. I have the following mappings:

Piece:
Code:
<class name="DAL.BusinessObjects.Piece, DAL" table="dbo.Piece" lazy="true">
      <id name="Id" column="PieceGuid">
          <generator class="assigned" />
      </id>
      <bag name="Piece2Games" lazy="true" cascade="all-delete-orphan"  inverse="true" >
         <key column="PieceGuid"></key>
         <one-to-many class="Piece2Game"></one-to-many>
      </bag>
   </class>


Piece2Game:
Code:
class name="DAL.BusinessObjects.Piece2Game, DAL" table="dbo.Piece2Game" lazy="true">
      <id name="Id" column="Piece2GameID">
          <generator class="native" />
      </id>
      <many-to-one name="PieceMember" column="PieceGuid" class="Piece" />
   </class>


When I load a Piece2Game object, nHibernate generates a proxy for Piece. If I only want to read PieceGuid of the Piece object (the FK value), it triggers a lazy load of the Piece object. Since PieceGuid is the FK, I already have the value and don't need to lazy load the entire object. Is there any way to prevent this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 8:01 pm 
Newbie

Joined: Wed Dec 03, 2008 11:23 am
Posts: 2
After some more digging on the Hibernate forum, I found an answer to my question.

It turns out that mant to one relationships are set to lazy='proxy' by default. Reading the FK value does not in fact cause a lazy load for the Piece object.

The reason I was seeing a load was that I had a watch on a property of the Piece object which was triggering the lazy load! The moral of the story here is to always profile your code outside the debugger before crying foul and blaming nHibernate.

--Eric


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