给出我的配置文件和代码,大家帮我看下为什么lazy不了。
很简单的两个pojo,father和child,关系是一对多。
项目名称是HiberDemo3, cs文件在Domain包中。
后面给出所有代码,大家直接复制我的代码,帮我看一下问题所在。
先说下得到的测试情况。
我测试代码得到生成的sql如下,可以看到还是进行连接了,我要延迟子类的载入,应该改哪个地方?NHibernate: SELECT this_.id as id1_1_, this_.fathername as fathername1_1_, children2_.father_id as father2___3_, children2_.id as id3_, children2_.id as id0_0_, children2_.father_id as father2_0_0_, children2_.childName as childName0_0_ FROM Father this_ left outer join Child children2_ on this_.id=children2_.father_id
father的配置<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HiberDemo3" namespace="HiberDemo3.Domain">
<class name="Father">
<id name ="id">
<generator class="native"/>
</id>
<set name ="Children" lazy="true" cascade="all-delete-orphan" inverse="true" outer-join="true">
<key column="father_id"></key>
<one-to-many class="Child"/>
</set>
<property name="fathername" type="string" length="8"></property>
</class>
</hibernate-mapping>child的配置<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HiberDemo3" namespace="HiberDemo3.Domain">
<class name="Child" lazy="true">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="father" class="Father" column="father_id"></many-to-one>
<property name="childName" type="string"/>
</class>
</hibernate-mapping>
Father的cs文件using System;
using Iesi.Collections; namespace HiberDemo3.Domain
{
/// <summary>
/// POJO for Father. This class is autogenerated
/// </summary>
[Serializable]
public  class Father
{
#region Fields

private Int32 _id;
private String _fathername;
private ISet children; #endregion #region Constructors

/// <summary>
/// Initializes a new instance of the Father class
/// </summary>
public Father()
{
}

/// <summary>
/// Initializes a new instance of the Father class
/// </summary>
/// <param name="_fathername">Initial <see cref="Father.fathername" /> value</param>
/// <param name="children">Initial <see cref="Father.Children" /> value</param>
public Father(String _fathername, ISet children)
{
this._fathername = _fathername;
this.Children = children;
}

/// <summary>
/// Minimal constructor for class Father
/// </summary>
/// <param name="children">Initial <see cref="Father.Children" /> value</param>
public Father(ISet children)
{
this.Children = children;
}
#endregion

#region Properties

/// <summary>
/// Gets or sets the id for the current Father
/// </summary>
virtual public Int32 id
{
get { return this._id; }
set { this._id = value; }
}

/// <summary>
/// Gets or sets the fathername for the current Father
/// </summary>
        virtual public String fathername
{
get { return this._fathername; }
set { this._fathername = value; }
}

/// <summary>
/// Gets or sets the Children for the current Father
/// </summary>
    virtual public ISet Children
    {
        get { return children; }
        set { children = value; }
    }     #endregion
}
}
child的cs文件using System;
using Iesi.Collections; namespace HiberDemo3.Domain
{
/// <summary>
/// POJO for Father. This class is autogenerated
/// </summary>
[Serializable]
public  class Father
{
#region Fields

private Int32 _id;
private String _fathername;
private ISet children; #endregion #region Constructors

/// <summary>
/// Initializes a new instance of the Father class
/// </summary>
public Father()
{
}

/// <summary>
/// Initializes a new instance of the Father class
/// </summary>
/// <param name="_fathername">Initial <see cref="Father.fathername" /> value</param>
/// <param name="children">Initial <see cref="Father.Children" /> value</param>
public Father(String _fathername, ISet children)
{
this._fathername = _fathername;
this.Children = children;
}

/// <summary>
/// Minimal constructor for class Father
/// </summary>
/// <param name="children">Initial <see cref="Father.Children" /> value</param>
public Father(ISet children)
{
this.Children = children;
}
#endregion

#region Properties

/// <summary>
/// Gets or sets the id for the current Father
/// </summary>
virtual public Int32 id
{
get { return this._id; }
set { this._id = value; }
}

/// <summary>
/// Gets or sets the fathername for the current Father
/// </summary>
        virtual public String fathername
{
get { return this._fathername; }
set { this._fathername = value; }
}

/// <summary>
/// Gets or sets the Children for the current Father
/// </summary>
    virtual public ISet Children
    {
        get { return children; }
        set { children = value; }
    }     #endregion
}
}
测试代码很简单:[Test]
public void testSome()

    ISession session = .....
    var l = session.CreateCriteria(typeof (Father)).List(); 
    Assert.Greater(l.Count, 0);
}