-->
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, not saving when inverse is set to true.
PostPosted: Wed Mar 16, 2011 3:15 am 
Newbie

Joined: Thu Nov 05, 2009 11:36 am
Posts: 6
I have what seems to be a very simple question but I can't seem to figure it out. If I have a one to many relationship between [Member] and [Mail]

If I take off inverse it all works, it's just when I call instance.inverse that it doesnt save.

Code:
public class Mail : Entity
    {
        [Cascade(Enums.CascadeOptions.All)]
        public virtual Member Receiver
        {
            get; set;
        }

        public virtual string Subject
        {
            get;
            set;
        }

        public virtual string Body
        {
            get; set;
        }
    }




Code:
public class Member : Entity
    {
        public Member()
        {           
            Mail = new List<Mail>();           
        }

        [Cascade(Enums.CascadeOptions.All)]
        public virtual IList<Mail> Mail
        {
            get; set;
        }
    }

Inverse is true on a one to many (through an automapping convention)

Code:
public class HasManyConvention : IHasManyConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
        {

            instance.Key.Column(instance.EntityType.Name + "ID");
            instance.Inverse();
}}

I try to add a mail item to a member through:

Code:
mail.Receiver = receiver;
        receiver.Mail.Add(mail);           
        _repository.SaveOrUpdate(mail);
        FlushSessionAndEvict(mail);


The cascade attribute is something I created to tell the automapper to cascade and it works. I also just set the cascade to all for this test no matter what the attribue is.


Now what's weird is the mail item is saved to the database, there is a foreign key called 'MemberId' which SHOULD have the foreign key to the parent member table and a column called ReceiverFk which is according to my convention but shouldn't be there if there is already a foreign key relationship with MemberId. When I retrieve the user it doesn't have the mail item attached to it. If I take off inverse everything works, the foregin key is saved in MemberId.

I don't want to take off inverse though because it entails another update call to the database. What can I do?

I can supply the maps the automapper generated.

Thanks


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.