-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Nhibernate saves collection correctly but can't retrieve it
PostPosted: Mon Jun 27, 2011 9:59 am 
Newbie

Joined: Thu Nov 05, 2009 11:36 am
Posts: 6
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.