NHibernate - 从同一个表映射父/子一对多关联
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到了它。我必须在 OneToMany 属性中使用完全限定的类名。
I got it. I had to use the fully qualified class name in the OneToMany attribute.