13

I have the following models & mappings (code snippets further below).

One Competition has to have multiple CompetitionAnswers associated with it (multiple choice) from the outset.

At present, using the Fluent NHibernate mappings shown below, when I create a brand new Competition object, populate the properties, then create 3 brand new CompetitionAnswer objects and add them to the CompetitionAnswers property (property on Competition), I would expect to call Save on the session which would INSERT the 1 Competition row and 3 CompetitionAnswer rows to the DB.

However, as soon as I try to call Save on the session, it complains that CompetitionId is null and it can't insert a null into the CompetitionAnswers table for that field - which is right, it shouldn't, however, I assumed that the NHibernate would first create the Competition, then use the newly generated IDENTITY value (CompetitionId) in the CompetitionAnswers table?

Competition (Model)

public virtual int CompetitionId { get; private set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual IList<CompetitionAnswer> CompetitionAnswers { get; set; }

CompetitionAnswer (Model)

public virtual int CompetitionAnswerId { get; set; }
public virtual string Answer { get; set; }
public virtual Competition Competition { get; set; }

CompetitionMap (Fluent NHibernate Mapping)

public CompetitionMap()
{
    Id(x => x.CompetitionId)
        .GeneratedBy.Native();
    Map(x => x.Title);
    Map(x => x.Description);
    HasMany(x => x.CompetitionAnswers)
        .Cascade.AllDeleteOrphan()
        .KeyColumn("CompetitionId")
        .Inverse();
    Table("Competitions");
}

CompetitionAnswerMap (Fluent NHibernate Mapping)

public CompetitionAnswerMap()
{
    Id(x => x.CompetitionAnswerId)
        .GeneratedBy.Native();
    Map(x => x.Answer);
    References(x => x.Competition)
        .Column("CompetitionId");
    Table("CompetitionAnswers");
}

Here is some sample code that I've used to test this scenario, which generates the error:

Competition c = new Competition();

c.Description = "Description";
c.Title = "Title";

CompetitionAnswer a1 = new CompetitionAnswer { Answer = "Answer 1" };
CompetitionAnswer a2 = new CompetitionAnswer { Answer = "Answer 2" };
CompetitionAnswer a3 = new CompetitionAnswer { Answer = "Answer 3" };

c.CompetitionAnswers.Add(a1);
c.CompetitionAnswers.Add(a2);
c.CompetitionAnswers.Add(a3);

session.Save(c);

The exact error that I get as soon as it tries to Save is:

Cannot insert the value NULL into column 'CompetitionId', table 'CompetitionAnswers'; column does not allow nulls. INSERT fails. The statement has been terminated.

Can anyone please shed any light on why this isn't currently working?

marcusstarnes
  • 6,393
  • 14
  • 65
  • 112

3 Answers3

22

I'm pretty sure, not 100%, that the problem is the Inverse() specification in your mapping of CompetitionAnswers on Competition. Inverse() specifies that the child records are responsible for defining their relationship to the parent. Most often, the "one" side of a one-to-many (the parent) is the "top" of an object graph and "owns" the relationship with its children. Parents have children, and the decision regarding whether to keep or give away the child for adoption is the parent's. However, this isn't always the case; a college may have students, but it's the students who have the real power to decide where they will go. Here, the Student is the "top" of the graph, and the School is just a monolithic record identifying the Student's attendance. The Student can transfer at any time; it's their decision, and it doesn't really change the School in any meaningful way, so the Students are responsible for identifying themselves as belonging to the School.

Your case is the first one: Competitions have CompetitionAnswers, and the child doesn't logically have the responsibility of saying "I belong to a Competition"; the Competition instead "owns" its collection of answers. Removing the Inverse() instruction should make NH treat Competition as the "top" of the object graph, so NH will insert the Competition, then the CompetitionAnswers, which can now reference their parent's ID.

Another thing not related to the problem, but if you're mapping to an MS SQL Server database, and the ID column is defined as an identity column in the DB, I'd specify GeneratedBy.Identity() for the ID columns. Native() SHOULD end up using Identity, but it will also check to see if HiLo or Sequence methods are available.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • 1
    The solution was actually a combination both KeithS's suggestion and Yads. I did have to allow NULL on the CompetitionId field in the DB, but that alone just allowed the records in 'without' the CompetitionId (it remained NULL). By removing Inverse() from the CompetitionAnswers mapping, it then updated the CompetitionAnswers records with the newly assigned CompetitionId. Thanks to both of you for helping sort this. – marcusstarnes Mar 09 '11 at 08:42
6
References(x => x.Competition)
    .Column("CompetitionId")
    .Not.Nullable(); //add this

and also make sure the child points to the parent in addition to the parent holding the child in it's collection. This is required when the parent is inverse.

a1.Competition = c; 

Was that column non null on database side but mapped as nullable on NH side?

dotjoe
  • 26,242
  • 5
  • 63
  • 77
0

There is nothing wrong with your mappings. The problem is likely with your database. If you're using Identity columns for your primary keys, the CompetitionId column should be set to nullable. The way NHibernate works when you have a brand new object with children that is saved, is it inserts the parent object and the child objects. Then it updates the child object foreign keys with the new parent object id.

I found this NHibernate cascading save

Community
  • 1
  • 1
Vadim
  • 17,897
  • 4
  • 38
  • 62
  • No. NH first inserts the parent object, and that operation results in the session now knowing the ID of that object (because it uses a system stored proc which will return the ID). Then NH saves the child objects. Foreign keys should only be nullable if it is actually valid for a record not to have that association long-term; the only case where a foreign key has to be nullable for persistence reasons is a one-to-one. – KeithS Mar 08 '11 at 17:56
  • @KeithS, actually no it doesn't. It generates the ID first, then it saves the children (cascade), then it saves the object. If the id cannot be generated until after an insert (eg. identity column on MS SQL server), then it will save the children with a null value for the foreign key, then update them with the proper value after the parent is inserted. – Vadim Mar 08 '11 at 18:14
  • Not if the table uses an identity column, which is true when it's mapped using GeneratedBy.Native() (as it is in the OP) and the DB supports identity columns. The DB produces all ID values except in certain cases of GUIDs. – KeithS Mar 08 '11 at 18:17
  • @KeithS, I just went through the code, and you're wrong. See `IdentityGenerator`, if you note it implements `IPostInsertIdentifierGenerator`. If you check `AbstractSaveEnventListener.SaveWithGeneratedId` when there is a PostInsert generator it specifies null for the id argument of the `PerformSave` method, which in turn specifies null for the key. Look at `PerformSaveOrReplicate`. If you note it sets the id to null then it does a `CascadeBeforeSave`, followed by an insert, followed by a `CascadeAfterSave` – Vadim Mar 08 '11 at 18:27
  • I did try allowing NULL values for the CompetitionId field in the CompetitionAnswers table earlier, and whilst it didn't error, and the 3 CompetitionAnswer records were written, no CompetitionId was included with those records - they remained NULL in the (SQL Server 2008) DB. I'm pretty sure I tried removing the Inverse() also and it produced the same error as I pasted in the OP, though I will have to try that again to make sure when I'm back at work in the morning. – marcusstarnes Mar 08 '11 at 18:37
  • @marcus you may need to commit the transaction or flush the session after you save to see the updated ids. – Vadim Mar 08 '11 at 19:34
  • @Yads - Everything you say may be correct; the point remains, you DO NOT have to specify nullable foreign keys on the many side just because you are using NHibernate. I know because I'm looking at the results of FluentNH generating the schema for a one-to-many relationship in the database for the app I'm developing, and the column with the FK reference to the one side isn't nullable. Your answer was the first time in 2 years of using NH that I'd ever heard someone say it had that limitation. NH does have limitations; requiring FKs on children to be nullable is NOT one of them. – KeithS Mar 08 '11 at 19:40
  • @Keith, this is not a limitation of NHibernate, but a limitation of the identity generator. Are you using identity columns on Sql server? I had NHibernate generate my schema with identity id generators and all FK key columns were indeed nullable. Set up a simple 2 class project, with a one to many relationship with identity id generators, set the FK column to be not nullable and see if you can get it to work. – Vadim Mar 08 '11 at 19:47
  • @Yads - Yes, I did commit the transaction and flush the session - sorry that's not apparent from my edited (for brevity) version of the test code in the OP. Despite this, it still left NULLS in the CompetitionId field in the CompetitionAnswers table. It must be one of the other things suggested here. I will try all of the suggestions in the morning and update accordingly. Thanks. – marcusstarnes Mar 08 '11 at 20:41