Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate Cascade Issue - Trying To Insert NULL ID

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?

like image 956
marcusstarnes Avatar asked Mar 08 '11 16:03

marcusstarnes


2 Answers

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?

like image 185
dotjoe Avatar answered Sep 21 '22 16:09

dotjoe


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.

like image 40
KeithS Avatar answered Sep 18 '22 16:09

KeithS