user1250264
user1250264

Reputation: 905

Getting "Error dehydrating property value for X type" error and TryUpdateModel and UpdateModel is not working

I am getting an "Error dehydrating property value for X type" I checked the mapping against the database table and all the single property match. In both my entity and nhiberate there are additional objects (many to one relationship). I am stuck on how to trace and fix the dyhyrdrating error. Also, TryUpdateModel and UpdateModel are not updating at all. Any help is appreciated.

<hibernate-mapping assembly="MY.BusinessEntities" namespace="MY.BusinessEntities.NHObjects" xmlns="urn:nhibernate-mapping-2.2">
<class name="MY_OBJECTION" table="MY_OBJECTION" lazy="true" >
  <id name="MY_OBJECTION_UID">
    <column name="MY_OBJECTION_UID" sql-type="NUMBER" not-null="true"  />
    <generator class="native">
      <param name="sequence">SEQ_MY_OBJECTION</param>
    </generator>
  </id>

  <many-to-one column="MY_RECORD_UID" name="MyRecordState" class="MyRecordState">
    <column name="MY_RECORD_UID" />
  </many-to-one>

  <many-to-one column="RECORD_TYPE" name="RECORD_TYPE" class="OFAC_LOOKUP" not-found="ignore" />
  <many-to-one column="OBJECTION_UID" name="OBJECTION" class="OBJECTION" not-found="ignore" />
  <property name="OBJECTION_UID" column="OBJECTION_UID" type="int" /> 
  <property name="MY_RECORD_UID" column="MY_RECORD_UID" type="int" />
  <property name="CASE_ID" column="CASE_ID" type="string"  />
  <property name="CASE_UID" column="CASE_UID" type="int" />
  <property name="RECORD_TYPE_UID" column="RECORD_TYPE" type="int" />
  <property name="REFERENCE_TO" column="REFERENCE_TO" type="int" />
  <property name="REMARKS" column="REMARKS" type="string" />
  <property name="BULK_ASSIGN" column="BULK_ASSIGN" type="char" />

  <component name="AuditInfo" class="AuditMetadata">
    <property name="DateCreated" column="DATE_CREATED" type="date" />
    <property name="CreatedBy" column="CREATED_BY" type="int"/>
    <property name="DateChanged" column="DATE_CHANGED" type="date" />
    <property name="ChangedBy" column="CHANGED_BY" type="int"/>
  </component>

</class>
</hibernate-mapping>

Entity:

namespace MY.BusinessEntities.NHObjects {

  public class MY_OBJECTION {
    public MY_OBJECTION() {}
    public virtual int MY_OBJECTION_UID { get; set; }
    public virtual MyRecordState MyRecordState { get; set; }

    public virtual OFAC_LOOKUP RECORD_TYPE { get; set; }
    public virtual OBJECTION OBJECTION { get; set; }
    public virtual int OBJECTION_UID { get; set; }

    public virtual int REFERENCE_TO { get; set; }
    public virtual string REMARKS { get; set; }
    public virtual char BULK_ASSIGN { get; set; }

    public virtual AuditMetadata AuditInfo { get; set; }

    public virtual int MY_RECORD_UID { get; set; }
    public virtual string CASE_ID { get; set; }
    public virtual int CASE_UID { get; set; }
    public virtual int RECORD_TYPE_UID { get; set; }
  }
}

Upvotes: 0

Views: 5107

Answers (2)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

Mapping issue

There is one obvious mapping issue, which is causing this type of error. In the snippet below I've 1) simplified 2) moved together and 3) adjsuted the order of mapping attributes to make it more clear:

<many-to-one column="RECORD_TYPE"   name="RECORD_TYPE" class="OFAC_LOOKUP" ... />
<property    column="RECORD_TYPE"   name="RECORD_TYPE_UID" type="int" />
...
<many-to-one column="OBJECTION_UID" name="OBJECTION"   class="OBJECTION" .../>
<property    column="OBJECTION_UID" name="OBJECTION_UID" type="int" /> 
...

This is a real problem for NHibernate. We are instructing him: during the INSERT and UPDATE the SQL should target one column twice: object reference and int to be stored into RECORD_TYPE resp. OBJECTION_UID

And that's from where the above problem comes (well maybe there are more, but this one I've spotted now)

Solution

In NHibernate we can mark some of these mappings as readonly. that will bring us many advantages. First of all, this error won't exist anymore, because during the INSERT and UPDATE we won't target these mappings. On the other hand, we still have access to readonly properties for Filtering/WHERE, Projections/SELECT and ORDER BY... cool

From my experience the prefered way is to keep references alive and their int representation readonly:

<many-to-one column="RECORD_TYPE"   name="RECORD_TYPE" class="OFAC_LOOKUP" ... />
<property    column="RECORD_TYPE"   name="RECORD_TYPE_UID" type="int" 
             insert="false" update="false" /> <!-- readonly now --> 
...
<many-to-one column="OBJECTION_UID" name="OBJECTION"   class="OBJECTION" .../>
<property    column="OBJECTION_UID" name="OBJECTION_UID" type="int" 
             insert="false" update="false" /> <!-- readonly now -->
...

Upvotes: 1

MichaC
MichaC

Reputation: 13390

PropertyValueException thrown by nh can have many reasons. You have to try to figure out which property is causing the issues...

All referenced types in the mapping must be mapped, too. I do not see any mappings for MyRecordState, OFAC_LOOKUP or OBJECTION.

For insert/updates you might have to set cascade and inverse options to be able to properly insert values...

Upvotes: 0

Related Questions