I have two classes, WallPost and Vote. WallPost contains a 1:n relationship with Vote. The classes are automapped in NHibernate.
Ok now I'm running tests and I noticed that I can save both of them to the db but when I flush the session and retrieve them, the collection of Votes in WallPost is empty, it doesn't reflect the db and it should be initialized.
Here is the test I run where I get an error on the assert:
Code:
//Creating a wallpost
WallPost post = new WallPost();
post.Author = memberTwo;
post.Receiver = memberOne;
post.BodyText = "a";
//Creating the Vote
var vote = new Vote();
vote.IsUpvote = true;
vote.Member = memberOne;
//Here is where I make the connection between the two
vote.Post = post;
post.Votes.Add(vote);
//I'm saving both because I really don't know which one to save,
//kinda confused about the whole inverse thing.
provider.SaveVote(vote);
provider.SavePost(post);
//And finally evicting the vars from the ISession
FlushSessionAndEvict(vote);
FlushSessionAndEvict(post);
//Works
Assert.IsTrue(new VoteRepository().GetAll()[0] != null);
//Worls
Assert.IsTrue(new VoteRepository().GetAll()[0].Post!= null);
//FAILS ON THIS ASSERT!!!!
Assert.IsTrue(new PostRepository().GetAll()[0].Votes.Count > 0);
For some reason the Wallpost doesn't initialize the collection of Votes.
The saving just opens a transaction, calls SaveOrUpdate and then closes the transaction.
When I close the transaction, I see that all the information is saved in the database with correct foreign keys, etc. It just isn't retrieved correctly.
Here is my WallPost class:
Code:
[Serializable]
public class WallPost : Post<WallPost>
{
}
[Serializable]
public abstract class Post<T> : Post, IHierarchy<T> where T: Post
{
public Post()
{
Children = new List<T>();
}
public virtual T Parent {
get;
set;
}
public virtual int Depth
{
get;
set;
}
public virtual IList<T> Children
{
get;
set;
}
}
[Serializable]
public abstract class Post : Entity
{
public Post()
{
CreateDate = DateTime.Now;
Votes = new List<Vote>();
}
public virtual string Name
{
get; set;
}
public virtual string BodyText
{
get; set;
}
public virtual Member Author
{
get; set;
}
public virtual Member Receiver
{
get;
set;
}
public virtual DateTime CreateDate
{
get; set;
}
[Cascade(Enums.CascadeOptions.SaveUpdate)]
public virtual IList<Vote> Votes
{
get;set;
}
}
And here is my Vote class:
Code:
[Serializable]
public class Vote : Entity
{
public virtual Member Member
{
get; set;
}
[Cascade(Enums.CascadeOptions.None)]
public virtual WallPost Post
{
get;
set;
}
public virtual bool IsUpvote
{
get; set;
}
}
I can add more code on request, it seems like I probably put too much anyways.
Thank you.
E