Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nHibernate, No row with the given identifier exists

I have a mapping along the lines of this.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model.Entities" schema="etl" assembly="Model" default-lazy="false">   <class name="Model.Entities.DataField, Model" table="mdm_field">     <id name="FieldId" column="field_id" type="int">       <generator class="native" />     </id>     <many-to-one name="KeyField" class="Model.Entities.Key, Model" column="field_id" />   </class> </hibernate-mapping> 

Now in the database the field_id in the mdm_field table sometimes has a value that does not exist in the related key_field table, so it is basically broken referential integrity. Because of this when I load the entity I get an error "No row with the given identifier exists". How do I configure the mapping to work with this situation so it will not die on this situation.

like image 755
Craig Avatar asked Mar 30 '09 03:03

Craig


2 Answers

Ok, I found the answer. Add the

not-found="ignore" 

attribute to the property KeyField:

<many-to-one name="KeyField" not-found="ignore" class="Model.Entities.Key, Model" column="field_id" /> 
like image 130
Craig Avatar answered Sep 18 '22 09:09

Craig


In my case the problem was because a foreign key constraint was not enforced by MyISAM engine and therefore one of the rows ended up pointing to a non-existant value and the proxy threw an exception. I would recommend checking your dataset is consistent in this case.

like image 40
Shagglez Avatar answered Sep 18 '22 09:09

Shagglez