Asp.net mvc NHibernate-子对象为空

Asp.net mvc NHibernate-子对象为空,asp.net-mvc,nhibernate,load,Asp.net Mvc,Nhibernate,Load,使用NHibernate 2.0,映射的NHibernate类属性,ASP.NET MVC创建留言板类型的应用程序 我有两个实体,Post和User。Post有一个所有者属性,它是用户的实例。它还有一个Replies属性,它是ISet类型的集合 其想法是,一篇帖子可以有一个级别的回复,就像一篇带有评论的SO帖子一样。我的MVC页面使用部分视图(MVC.ViewUserControl类型)来显示顶级(父级)帖子。ParentPost部分视图依次使用ChildPost部分视图(也是Mvc.ViewU

使用NHibernate 2.0,映射的NHibernate类属性,ASP.NET MVC创建留言板类型的应用程序

我有两个实体,Post和User。Post有一个所有者属性,它是用户的实例。它还有一个Replies属性,它是ISet类型的集合

其想法是,一篇帖子可以有一个级别的回复,就像一篇带有评论的SO帖子一样。我的MVC页面使用部分视图(MVC.ViewUserControl类型)来显示顶级(父级)帖子。ParentPost部分视图依次使用ChildPost部分视图(也是Mvc.ViewUserControl类型)来显示回复(回复的不同显示标记)

一切都很好,除了repress集合中User类型的所有者实例为null之外。它们在父集合中加载得很好

换句话说,在父级,所有属性都被正确加载,包括帖子的所有者。在“回复”集合中,回复帖子将加载除其所有者之外的所有属性。不管它值多少钱,所有者是Post的唯一类属性

有人能帮我弄清楚如何让NHibernate在Replies集合中加载所有者实例吗

以下是Post的相关映射:

[Class(Table="t_Posts",Lazy=false)]
public class Post : IPost
{
    [Id(Name = "PostId")]
    public virtual long PostId { get; set; }

    [Property(Column="OwnerID")]
    public virtual long OwnerId { get; set; }

    [Property(Column="DatePosted")]
    public virtual DateTime DatePosted { get; set; }

    [OneToOne(0,ForeignKey="OwnerId",Lazy=Laziness.False,ClassType=typeof(User))]
    public virtual IUser Owner { get; set; }

    [Property(Column="ParentID")]
    public virtual long ParentId { get; set; }

    [Set(0,Name="Replies",Inverse=true,Cascade="all-delete-orphan", Lazy=false)]
    [Key(1,Column="ParentId")]
    [OneToMany(2,ClassType=typeof(Post))]
    public virtual ISet<Post> Replies{ get; set; }
}

对于Post.Owner,您应该将其映射为多对一。我的XML示例

<many-to-one name="Owner" lazy="false" column="OwnerId"/>

一对一映射将假定两个实体具有相同的ID值。 需要注意的另一点是,NH映射中的ForeignKey表示FK的名称,而不是外键列

这里有一个友好的提示,您不需要OwnerId和Owner属性。你所需要的只是所有者的财产。您也不需要ParentId属性,但请确保在Post.repress上设置Inverse=false

完整示例:

public class Post
{
    public virtual long PostId { get; set; }
    public virtual DateTime DatePosted { get; set; }
    public virtual User Owner { get; set; }
    public virtual ISet<Post> Replies { get; set; }

    public Post()
    {
        Replies = new HashedSet<Post>();
    }
}

public class User
{
    public virtual long UserId { get; set; }
    public virtual string LoginName { get; set; }
}
公共类职位
{
公共虚拟长PostId{get;set;}
公共虚拟日期时间DatePosted{get;set;}
公共虚拟用户所有者{get;set;}
公共虚拟ISet回复{get;set;}
公职人员职位()
{
回复=新的HashedSet();
}
}
公共类用户
{
公共虚拟长用户ID{get;set;}
公共虚拟字符串登录名{get;set;}
}
波斯特地图

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Mfs.Core"
                   assembly="Mfs.Core">
  <class name="Post" table="tPost" lazy="false">
    <id name="PostId">
      <generator class="hilo"></generator>
    </id>

    <property name="DatePosted" type="timestamp"/>

    <!--<one-to-one name="Owner" lazy="false" foreign-key="OwnerId"/>-->
    <many-to-one name="Owner" lazy="false" column="OwnerId"/>

    <set name="Replies" inverse="false" cascade="all-delete-orphan" lazy="false">
      <key column="ParentId"/>
      <one-to-many class="Post" />
    </set>
  </class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Mfs.Core"
                   assembly="Mfs.Core">
  <class name="User" table="tUser" lazy="false">
    <id name="UserId">
      <generator class="hilo"></generator>
    </id>
    <property name="LoginName"></property>
  </class>
</hibernate-mapping>

用户映射

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Mfs.Core"
                   assembly="Mfs.Core">
  <class name="Post" table="tPost" lazy="false">
    <id name="PostId">
      <generator class="hilo"></generator>
    </id>

    <property name="DatePosted" type="timestamp"/>

    <!--<one-to-one name="Owner" lazy="false" foreign-key="OwnerId"/>-->
    <many-to-one name="Owner" lazy="false" column="OwnerId"/>

    <set name="Replies" inverse="false" cascade="all-delete-orphan" lazy="false">
      <key column="ParentId"/>
      <one-to-many class="Post" />
    </set>
  </class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Mfs.Core"
                   assembly="Mfs.Core">
  <class name="User" table="tUser" lazy="false">
    <id name="UserId">
      <generator class="hilo"></generator>
    </id>
    <property name="LoginName"></property>
  </class>
</hibernate-mapping>

啊,谢谢!就这样。第一次和NHibernate分手,我都快发疯了。我想我会回去再看一遍协会的章节:)顺便说一句-谢谢你关于所有者ID的说明。我知道我不需要它,它就在那里,从那时起,我只是想让帖子在进入关联路径之前加载。再次感谢!!!