开发者

Nhibernate lazy loading a set not working

开发者 https://www.devze.com 2023-04-12 20:42 出处:网络
I\'m using nhibernate2.1 as part of spring.net 1.3.I have the following declaration as part of my mapping.My understanding is that this object should not load unless the getter is called.I have a brea

I'm using nhibernate2.1 as part of spring.net 1.3. I have the following declaration as part of my mapping. My understanding is that this object should not load unless the getter is called. I have a break point set on the setter and also dump all nhibernate SQL statements to the logger. In part of my testing, I've actually created 开发者_JAVA技巧a brand new child object and a brand new property on my original object (hence the "2" on the names) so I'm positive that property is not being accessed anywhere. Despite this, as soon as my parent object loads, I can verify that this property is loaded. So...what am I missing here?

<set name="UserCustomer2" lazy="true">
    <key column="[FK_USERS]" />
    <one-to-many class="UserCustomer2" />
</set>

@A: here is my property:

private ICollection<UserCustomer2> _UserCustomer2 = new HashSet<UserCustomer2>();
public virtual ICollection<UserCustomer2> UserCustomer2
{
    get { return _UserCustomer2; }
    set { this._UserCustomer2 = value; }
}

and here is how I request the parent object:

IQuery query = dao.GetQuery("FROM UserImpl u WHERE u.UserName = :username AND u.Password = :password");
query.SetParameter("username", username);
query.SetParameter("password", password);
IList users = query.List();


@dotjoe, you led me down the right path. I tested out quite a few scenarios and based on breakpoints and the logged SQL statements I've determined that setting breakpoints does NOT trigger the lazy load. However, when you hover over the property to inspect it while in debug mode, it DOES in fact hydrate the object. I guess that sort of makes sense but I was assuming when I inspected the object, I would just see a proxy object instead of the fully hydrated object. I'm surprised that a debug mode action would trigger the lazy load -- I assumed that would only be triggered from actual application code. The other thing I'm curious about is if the setter is ALWAYS called and just passed a proxy object or if it was only called BECAUSE I had put a breakpoint in the setter. I would assume the former but so far my assumptions have been wrong. If anyone can provide some insights, I'm very curious how this actually works behind the scenes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号