views:

574

answers:

1

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?

+1  A: 

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; }
squillman

related questions