NHibernate - 从同一个表映射父/子一对多关联

发布于 2024-08-06 08:04:44 字数 1018 浏览 7 评论 0原文

我对 NHibernate 还很陌生,并且在让这种映射工作时遇到问题。我正在使用 NHibernate 2.1.0.GA 和 NHibernate.Mapping.Attributes 2.0。

我有一个与自身相关的表(t_Posts)作为父/子关系:

t_Posts
------------------------
(PK) PostID bigint
     DatePosted datetime
     Body nvarchar(1000)
(FK) ParentPostID bigint

我希望在类(Post)上有一个属性(Children),它是一组子帖子。我正在使用类属性进行映射并拥有它。

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

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

    [Property(Column="Body")]
    public virtual string Body { get; set; }

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

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

但是,当我运行此命令时,我收到异常“关联引用未映射的类:Post”。我不能在同一个班级里这样做吗?

I'm pretty new to NHibernate and am having a problem getting this kind of mapping to work. I'm using NHibernate 2.1.0.GA and NHibernate.Mapping.Attributes 2.0.

I have a single table (t_Posts) related to itself as a parent/child relationship:

t_Posts
------------------------
(PK) PostID bigint
     DatePosted datetime
     Body nvarchar(1000)
(FK) ParentPostID bigint

I would like to have a property (Children) on a class (Post) that is a set of child posts. I'm using class attributes for the mapping and have this.

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

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

    [Property(Column="Body")]
    public virtual string Body { get; set; }

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

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

When I run this, however, I get the exception "Association references unmapped class: Post". Can I not do this within the same class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-08-13 08:04:45

我得到了它。我必须在 OneToMany 属性中使用完全限定的类名。

[Set(0,Name="Children",Inverse=true,Cascade="all-delete-orphan", Lazy=true)]
[Key(1,Column="ParentId")]
[OneToMany(2,Class="MyProj.Domain.Post")]
public virtual ISet<Post> Children { get; set; }

I got it. I had to use the fully qualified class name in the OneToMany attribute.

[Set(0,Name="Children",Inverse=true,Cascade="all-delete-orphan", Lazy=true)]
[Key(1,Column="ParentId")]
[OneToMany(2,Class="MyProj.Domain.Post")]
public virtual ISet<Post> Children { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文