-->
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: lazy loading of n-to-1 relationship
PostPosted: Fri May 13, 2005 3:27 pm 
Newbie

Joined: Fri May 13, 2005 3:04 pm
Posts: 6
Location: Cleveland, OH
I'm having problems getting a n-to-one relationship to work via lazy activation. Basically, I have a User class which has a <many-to-one> property to an Address.
The mapping def for the relationship in User looks like:
Code:
<many-to-one name="UserAddress"
    class="NHibernateTestModel.Address, NhibernateTestModel"
    column="addID" cascade="all"/>


The Address class mapping contains the lazy="true" attribute:
Code:
<class name="NHibernateTestModel.Address, NhibernateTestModel" table="Address" lazy="true">
    <id name="ID" column="addID" type="Int32" unsaved-value="0">
        <generator class="assigned" />
    </id>
    <property name="Address1" column="Address1" type="String" length="255"/>
    <property name="City" column="City" type="String" length="255"/>
    ...

</class>


In the test application, I access the address like this:
Code:
User user = (User)session.Load(typeof(User), "john");
Address addr = user.UserAddress;
string street = addr.Address1;


The returned address object ("addr") is an instance of CProxyTypeAddress_ISerializable_INHibernateProxy2. When invoking the property accessor Address1, no access to the database is ever done to fetch the Address instance, and the value null is returned. If I remove the "lazy=true" line to the Address class mapping, the address object is fetched with the user but everything is returned correctly.

Any idea what I'm doing wrong here?

thanks,
--john


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 1:55 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Did you make all the properties of Address virtual? If they are not virtual, there is no way to intercept the call to them and load the object.


Top
 Profile  
 
 Post subject: What?
PostPosted: Wed May 25, 2005 10:59 am 
Regular
Regular

Joined: Tue May 24, 2005 12:55 pm
Posts: 56
I've been trying to figure this out. I've found nothing stating that if a class is lazy that the props need to be virtual. Is there more info about this? If so, could someone please post a url(s).
Quote:
Nevermind, found a link to the FAQ on JIRA that has a short blurb about proxies/lazy loading:
http://wiki.nhibernate.org/display/NH/Frequently+Asked+Questions


Last edited by tinomen on Wed May 25, 2005 12:08 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 11:56 am 
Newbie

Joined: Mon May 16, 2005 5:58 am
Posts: 11
Location: Galicia, Spain
jpompeii3, I have the same problem with my classes. The proxy must be transparent, but I think that it isn't

Any idea??


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 4:00 pm 
Newbie

Joined: Fri May 13, 2005 3:04 pm
Posts: 6
Location: Cleveland, OH
Sergey was right. I had to put virtual on the property definitions in order to make this work. Once I did that, everything worked great.

BTW, Sergey, is there any real downside to adding lazy="true" on all class definition?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 10:18 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
jpompeii3 wrote:
Is there any real downside to adding lazy="true" on all class definition?


You have to make sure you initialize all the proxies you will need before closing the session (ore reattach it to an opened session before)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 4:22 pm 
Newbie

Joined: Fri May 13, 2005 3:04 pm
Posts: 6
Location: Cleveland, OH
KPixel wrote:
jpompeii3 wrote:
Is there any real downside to adding lazy="true" on all class definition?


You have to make sure you initialize all the proxies you will need before closing the session (ore reattach it to an opened session before)


I'm not sure what you're trying to say. Basically what I'm asking is that the lazy="true" on the class tags enables the proxying of the class when it's on the end of a one to one or many to one. If a class is not going to be proxied, does setting lazy="true" on the class tag for these classes matter. That is, is it ignored if proxying for that class is not needed.

The reason for asking is that we're generating the hbm.xml files from a UML model and it's a pain to know when setting lazy="true" is required on a class and it's subclasses


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 3:15 pm 
KPixel wrote:
jpompeii3 wrote:
Is there any real downside to adding lazy="true" on all class definition?


You have to make sure you initialize all the proxies you will need before closing the session (ore reattach it to an opened session before)



How does one reattach an object graph to a new session?

I am running into this issue in an ASP.NET environment where I am storing my objects into the viewstate and retreiving them on PostBack. After postback I am trying to access linked objects that I hadn't accessed on the initial page load and am getting the "no session" exception. I don't want to save any of the objects yet, as I have a multi-step wizard process (long transaction) where nothing should be saved to the DB until the final step.


Top
  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 8:39 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Code:
session.Lock(myEntity, LockMode.None);

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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.