I want to have a foreign key that can be null: a child has zero or one parents.
NHibernate always complains about Foreign key reference target does not exist
. Is it even impossible to do what I want?
Child:
References( x => x.Parent ).Column("PARENT_ID").Cascade.SaveUpdate().Nullable().NotFound.Ignore();
Parent:
HasMany(x => x.Structures).KeyColumn("PARENT_ID").Cascade.SaveUpdate().Inverse().KeyNullable().NotFound.Ignore();
The generated XML for the child:
<many-to-one cascade="save-update" class="..." name="..." not-found="ignore">
<column name="PARENT_ID" not-null="false">
</many-to-one>
And for the parent:
<bag cascade="save-update" inverse="true" name="...">
<key not-null="false">
<column name="PARENT_ID" />
</key>
<one-to-many class="..." not-found="ignore" />
</bag>
The log outputs
INSERT INTO CHILD (..., PARENT_ID, ..) VALUES (..., @p1, ...)
Parameters: Name:@p1 Type:BigInt Used Value:False
I don't know how the parent ID can be a boolean, anyway, but I guess this is not the problem and just an internal representation of the value? No syntax exception is thrown.
In the DB tool, the query executes and the data is inserted.
I'm using NHibernate 3.3.3.40000, FluentNHibernate 1.3.0.733 with Firebird as Database and ADO.NET provider 3.0.2.1.