One-Many Requiring Nullable Foreign Keys
Description
Environment
is related to
Activity

Henrik Uffe Jensen August 1, 2011 at 8:56 PM
It's basically working, but it still do the sql updates too, even though the foreign key is inserted correctly doing the inserts. It can be seen by profiling the unit tests in NH 3.2 made especially for this issue.

Fabio Maulo July 30, 2011 at 2:11 PM
Closed after final release of NH3.2.0GA

Theo de Roo August 21, 2010 at 7:41 AM
We have had some odd surprises with this issue with our system. Have a Flight object with multiple Booking objects and all Bookings can have multiple Invoices, plus the flight can have a virtual Invoice. The problem is that for ease of reporting we have a FlightID column in the Invoice which is not nullable.
Things seem fine when the flight is saved to the DB before the first invoice gets created. However, when both the flight bookings and invoices are created together and we then try to save the complete flight structure then suddenly we get this Invoice.FlightID does not allow NULL exception.
Currently it seems that the only way around it is to save the Flight before creating the first invoice, or making this column Nullable (which it really shouldn't be) Also I don't get why this is a problem because all the relations are in place and the Flight already has an ID before the first save happens.
Puzzled user...

Colin Jack June 13, 2008 at 12:56 PM
Took me a while to get my head around this issue again, I think my example is over-complicated.
Anyway it isn't a major issue, its just that if it was easy to change the behavior to allow these FK to be non-nullable then it might be useful particularly when using NHibernate with a legacy database.

Fabio Maulo June 13, 2008 at 10:35 AM
Are you sure this is an issue ?
FeeTemplateDefinitionID is not a FK but it is the PK of FeeTemplateDefinition table.
I really have some difficult to understand well what you need...
Details
Details
Assignee
Reporter

We are working with a legacy database and due to dependencies (DTS/reports etc) it is very difficult to redesign.
We are also trying to balance good database design with good domain driven design, hoping to find a good balance between the two.
One place that this falls down is when mapping a one-many where the key is in the many end. Regardless of whether this is good database design we do have to deal with it quite a lot in our database and it cannot be engineered out easily, we thus have to map our domain classes tothe database it is now. Unfortunately this means making the foreign key nullable which is obviously a big step back in terms of database quality.
I've read the forum entries about this, including [url]http://forum.hibernate.org/viewtopic.php?t=957582&highlight=foriegn+foreign+key+nullable/url, but would like to suggest that you try to add support for non-nullable foreign keys in these situations as it would be a big help when working with existing databases.
[b]Hibernate version:/b 1.2
[b]Mapping documents:
Main Table /b
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="SevenIM.FeeSettings.Domain.FeeTemplateDefinition, SevenIM.FeeSettings.Domain" table="FeeTemplateDefinition" lazy="false">
<id name="Id" column="FeeTemplateDefinitionID">
<generator class="identity" />
</id>
<version name="_version" column="Version" access="field" />
<property name="Description" column="Description" access="nosetter.camelcase-underscore"/>
<!-- need to map to fee template -->
<bag name="_feeCalculationSettingTemplates" access="field" cascade="all-delete-orphan" lazy="true">
<key column="FeeTemplateSettingID" />
<one-to-many class="SevenIM.FeeSettings.Domain.FeeCalculationSettingTemplate, SevenIM.FeeSettings.Domain" />
</bag>
</class>
</hibernate-mapping>
[b]Sub Table /b
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="SevenIM.FeeSettings.Domain.FeeCalculationSettingTemplate, SevenIM.FeeSettings.Domain" table="FeeTemplate" lazy="false">
<id name="Id" column="FeeTemplateID">
<generator class="identity" />
</id>
<version name="_version" column="Version" access="field" />
<property name="Kind" column="FeeTypeID" access="nosetter.camelcase-underscore"/>
</class>
</hibernate-mapping>
[b]Exception: /b
Test method SevenIM.FeeSettings.Persistence.Tests.FeeTemplatePersistenceTests.CanPersist threw exception: NHibernate.ADOException: could not insert: [SevenIM.FeeSettings.Domain.FeeCalculationSettingTemplate] ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'FeeTemplateDefinitionID', table 'CRM.dbo.FeeTemplate'; column does not allow nulls. INSERT fails.
The statement has been terminated..