-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Foreign Key Constraint Issue When Saving Data
PostPosted: Mon Aug 15, 2011 7:01 am 
Newbie

Joined: Mon Aug 15, 2011 5:40 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.