I have two test tables STUDY and EXPERIMENT Experiments belong to Studies, so the EXPERIMENT table has a foreign key contraint STUDY_ID (primary key from STUDY table) When creating a foreign key constraint in the mapping file, <bag name="Experiments" cascade="all-delete-orphan"> <key column="STUDY_ID" not-null="true" foreign-key="FK_EXPERIMENT_STUDY"/> <!--Foreign Key in Experiment Table--> <one-to-many class="Experiment"/> </bag> if I set the not-null="true" for the constraint, I get an error on saving the a study with experiments. {"ORA-01400: cannot insert NULL into (\"MYDATABASE\".\"EXPERIMENT\".\"STUDY_ID\")"} Basically, the foreign key STUDY_ID in the Experiment Table is correctly preventing NHibernate from inserting a null value into it. It seems that NHibernate, is saving the child first 'Experiment' then saving the parent 'Study' and then trying to update the foreign key STUDY_ID in the Experiment Table If I remove the not-null="true" for the constraint, then it all works ... BUT I need it constrained. my C# code is like this... Study study = new Study(); study.Name = "Test Study"; study.Description = "Test Study Description"; Experiment experiment = new Experiment{ }; experiment.Name = "Test Experiment"; experiment.Description = "Test Experiment Description"; study.Experiments = new List<Experiment> {experiment}; session.Save(study);
Any Ideas would be appreciated.
|