-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help needed with many-to-one nullable relationship
PostPosted: Tue Jan 08, 2008 8:18 am 
Newbie

Joined: Tue Jan 08, 2008 7:58 am
Posts: 3
Hey guys,

Hoping you can help me with this problem. it's really starting to get a little annoying. I'm sure it's something i'm missing in the mapping files, but i just can't seem to sort it.

Basically I've got a table that has a NULLABLE foreign key. As far as i know the mapping files for both tables have been set-up properly (you can see each of these below). Each table knows of the other etc. (many-to-one and one-to-many)... however, when i try and save the many-to-one table WITHOUT a value set in the foreign key table it fails giving a standard SQL error message saying that there was a foreign key constraint problem.

This is of course an error message i would expect to receive if either:

a) the foreign key did NOT accept nulls or
b) i had set the foreign key ID to one that doesn't exist

Neither of these are true. in fact to make sure i've set the objectA.objectB = null. just in case nhibernate is trying to set it's value to zero or something.

Any ideas people? I'm sure it's just something i've missed in the mapping files.... which can be seen below:

ContentField.xml

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="App.Domain.Classes" assembly="App.Domain" default-access="property">
   <class name="App.Domain.Classes.EntityField, App.Domain" table="EntityField">
      <id name="Id" type="Int32" unsaved-value="null">
         <column name="EntityFieldID" length="4" sql-type="int" not-null="true" unique="true" index="PK_ContentEntryField"/>
         <generator class="identity" />
      </id>
      <many-to-one name="ContentEntry" class="App.Domain.Classes.ContentEntry, App.Domain">
         <column name="ContentEntryId" length="4" sql-type="int" not-null="false"/>
      </many-to-one>
   </class>
</hibernate-mapping>



ContentEntry.xml

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="App.Domain.Classes" assembly="App.Domain" default-access="property">
   <class name="App.Domain.Classes.ContentEntry, App.Domain" table="ContentEntry">
      <id name="Id" type="Int32" unsaved-value="null">
         <column name="ContentEntryId" length="4" sql-type="int" not-null="true" unique="true" index="PK_ContentEntry"/>
         <generator class="identity" />
      </id>
      <bag name="EntityFields" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="ContentEntryId"/>
         <one-to-many  class="App.Domain.Classes.EntityField, App.Domain"/>
      </bag>
   </class>
</hibernate-mapping>





It is the ContentField table that i am trying to create a record for without the ContentEntry foreign key set...

Looking forward to your replies and help.

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 1:23 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
I suspect that the problem is that the foreign key that you want to be null is not a nullable type, i.e., it's an int. So it's being persisted as a zero, which is an invalid foreign key value.

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 1:25 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Try to look at the SQL nHibernate Executes and see which value is supplied for your NULLABLE parameter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 4:00 pm 
Newbie

Joined: Tue Jan 08, 2008 7:58 am
Posts: 3
If a zero is being applied for some reason, what do you suggest i do? Can you suggest what additional parameters i would need to add to the mapping xml file?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 4:32 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
mabraham wrote:
I suspect that the problem is that the foreign key that you want to be null is not a nullable type, i.e., it's an int. So it's being persisted as a zero, which is an invalid foreign key value.


But NH is smart enough to figure that out for an FK on a many-to-one. This is something that should work out of the box, so something else must be going on. Configure log4net to get all the SQL that NH is executing (it's all under the NHibernate.SQL logger).

The only thing fishy about your mappings is that they seem way overdone. Try paring them down to default and detected settings and see if that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 4:39 pm 
Newbie

Joined: Tue Jan 08, 2008 7:58 am
Posts: 3
From what you can see is there anything specific I might be doing wrong in the mapping file? Or do you just think its a case of them potentially being over engineered? Thanks for your help.


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

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.