Using NHibernate 2.0, NHibernate class attributes for mappings, ASP.NET MVC to create a message board type app.
I have 2 entities, Post and User. Post has an Owner property which is an instance of User. It also has a Replies property which is a collection of type ISet.
The idea is that one post can have one level of replies, the same as a SO post with comments. My MVC page use a partial view (of type Mvc.ViewUserControl) to display the top level (parent) posts. The ParentPost partial view in turn uses a ChildPost partial view (also of type Mvc.ViewUserControl) to display the replies (different display markup for the replies).
Everything works great, except that the Owner instances of type User are null in the Replies collection. They get loaded just fine in the parent collection.
In other words, at the parent level all properties are loaded correctly, including the owner of the post. In the Replies collection, the reply posts are loaded with all properties except for their owners. For what it's worth, Owner is the only class property of Post.
Can someone help me to figure out how to make NHibernate load the Owner instances in the Replies collection?
Here is the relevant mapping for 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; }
}
Here is the relevant mapping for User:
[Class(Lazy=false,Table="t_Users")]
public class User : IUser
{
[Id(Name="UserId")]
public virtual long UserId { get; set; }
[Property(Column="LoginName")]
public virtual string LoginName { get; set; }
}