18

I'm trying to set up a relationship as follows. Each Master item has one or more Detail items:

public class Detail {
    public virtual Guid DetailId { get; set; }
    public virtual string Name { get; set; }
}
public class Master {
    public virtual Guid MasterId { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Detail> Details { get; set; }
}

And Mappings:

public class MasterMap : ClassMap<Master> 
{
    public MasterMap() 
    {
        Id(x => x.MasterId);
        Map(x => x.Name);
        HasMany(x => x.Details).Not.KeyNullable.Cascade.All();
    }
}
public class DetailMap : ClassMap<Detail> 
{
    public DetailMap() 
    {
        Id(x => x.Id);
        Map(x => x.Name);
    }
}

The Master database table is:

masterId   uniqueidentifier NOT NULL
name       nvarchar(max) NULL

and Detail is:

DetailId   uniqueidentifier NOT NULL
name       nvarchar(max) NULL
MasterId   uniqueidentifier NULL
foreign key (masterId) references [Master]

I don't really care to have a link from Detail back to Master -- in otherwords, Detail objects on their own are just not interesting to my domain layer. They will always be accessed via their Master object.

Using code like this:

Master mast = new Master 
{
    MasterId = new Guid(),
    Name = "test",
    Details = new List<Detail> 
    {
        new Detail { .DetailId = new Guid(), .Name = "Test1" },
        new Detail { .DetailId = new Guid(), .Name = "Test1" }
    }
};

using (transaction == Session.BeginTransaction) 
{
    Session.Save(mast);
    transaction.Commit();
}

This works great, except for a crazy limitation outlined in this post: NHibernate does an INSERT and puts Detail.MasterId as NULL first, then does an UPDATE to set it to the real MasterId.

Really, I don't want Detail entries with NULL MasterIds, so if I set the MasterId field to NOT NULL, the INSERT to Detail will fail, because as I said NHibernate is trying to put in MasterId = NULL.

I guess my question boils down to this:

How can I get the above code sample to work with my existing domain model (eg, without adding a Detail.Master property), and the Detail.MasterId field in the database set to NOT NULL?

Is there a way to get Nhibernate to just put the correct MasterId in the initial INSERT, rather than running an UPDATE afterwards? Is there rationale somewhere for this design decision? -- I'm struggling to see why it would be done this way.

3 Answers 3

33

NH3 and above allow to correct save entities in case of uni-directional one-to-many mapping without annoying save null-save-update cycle, if you set both not-null="true" on <key> and inverse="false" on <one-to-many>

FluentNHibernate code snippet for that:

public class MasterMap : ClassMap<Master> 
{
    public MasterMap() 
    {
        Id(x => x.MasterId);
        Map(x => x.Name);
        HasMany(x => x.Details)
            .Not.Inverse()     //these options are very
            .Not.KeyNullable() //important and work only if set together 
            .Not.KeyUpdate()   //to prevent double update
            .Cascade.All();
    }
}
2
  • 1
    Are you sure it doesn't issue a repetitive update? I set up my mapping as outlined and it sets the foreign key correctly on the insert (as opposed to inserting null), but then it does an additional database update where it resets the foreign key to the same value it used in the insert. Looks like VikciaR is experiencing and a couple others (based on upvotes) are experiencing the same thing.
    – Sam
    Commented Dec 17, 2012 at 21:49
  • 1
    queries work as expected, but nhibernate still creates a column on the db which allows nullable values? Is this normal?
    – nemenos
    Commented Oct 29, 2015 at 8:24
14

You can't. To quote the link from my answer on the other question you linked to:

Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.

Edit: as Hazzik has rightly pointed out, this has changed in NHibernate 3 and above. The docs sadly haven't been updated, so here's Hazzik:

[If you] set inverse="false" and not-null on <key>, NH3 and above will perform only two inserts insead of insert-insert-update.

5
  • I did read that, but it doesn't really explain anything (neither does the discussion of bidirectional associations referenced). Is there rationale behind this decision? Is it a design limitation of nhibernate? Is it a bug? As a relatively newbie to nhibernate, and not knowing the internals at all, it seems like this should be possible..
    – gregmac
    Commented Dec 17, 2010 at 16:31
  • Honestly, I don't know. It isn't classed as a bug by the NHibernate team, it's just a side-effect of how NHibernate persists entities. I won't pretend to know the reasons, but I imagine it has something to do with being database and identity generator agnostic. Commented Dec 17, 2010 at 16:42
  • 4
    Actually if set inverse="false" and not-null on key NH3 and above will perform only two inserts insead of insert-insert-update
    – hazzik
    Commented Sep 29, 2011 at 17:39
  • 3
    I don't know why, but if I set .Not.KeyNullable().Not.Inverse() insert works as expected - sets foreign key, but then later nhibernate issue extra update, which is don't needed: sets foreign key which already is set... Is this designed to be this way?
    – VikciaR
    Commented Dec 23, 2011 at 11:08
  • +1 VikciaR, same problem here. Proper insert with valid foreign key, but UPDATE still occurs.
    – Sam
    Commented Dec 17, 2012 at 22:51
4

The reason NHibernate does it this way is because:
When it saves the detail it only knows about the stuff the detail knows about. So any master references which happen in the background are ignored. Only when the master is saved it sees the relation and updates the elements of the collection with the id of the master.
Which is from an object oriented point of view logical. However from a saving point-of-view is slightly less logical. I suppose you can always file a bug report, or look if it might have been filed already and ask them to change it. But I suppose they have their specific (design/domain) reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.