Search code examples
c#nhibernatenhibernate-mappingone-to-one

one-to-one mapping in Nhibernate 3


I have the following classes

namespace Extractor.Mapping
{
    public class Application
    {
        public virtual Guid Id { get; set; }
        public virtual string intId { get; set; }
        virtual SQuery sQuery { get; set; }
    }
}


namespace Extractor.Mapping
{
    public class SQuery
    {
        public virtual int Id { get;set; } 
        public virtual Guid Application { get;set; } 
        public virtual string Type { get;set; }
    }
}

The relation between SQuery and Application is the FK in SQuery named "Application"

these are de hbm files

Application.hbm.xml

<hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2"
                    assembly="Extractor"
                    namespace="Extractor.Mapping">

  <class name="Application" >
    <id name="Id" column="Id">
      <generator class="assigned"/>
    </id>
    <property name = "intId" column = "IntId" type = "string"/>
    <one-to-one name="sQuery" property-ref ="Application" class="SicQuery"/>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>

SQuery.hbm.xml

<hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2"
                    assembly="BuroExtractorD"
                    namespace="BuroExtractorD.Mapping">
  <class name="SQuery" >
    <id name="Id" column="Id">
      <generator class="increment"/>
    </id>
    <property name="Application" column="Application" type ="Guid"/>
    <property name="Type" column="Type" type="string"/>
  </class>
</hibernate-mapping>

So, the thing is this, when i try to access a list of some SQuery objects, I get the following exception:

NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Extractor.Mapping.SQuery ---> System.InvalidCastException

I try to get there like this:

ICriteria crit = session.CreateCriteria(typeof(SQuery));
IList<SQuery> list = crit.List<SQuery>();

Thanks in advance


Solution

  • The point of the one-to-one is reference relation. This kind of mapping cannot be about ValueTypes.

    public class Application
    {
        public virtual SQuery sQuery { get; set; }
        ...
    }
    public class SQuery
    {
        // wrong, Guid is ValueType, not a reference 
        // public virtual Guid Application { get;set; } 
        // corrrect - one to one == bi-directional 
        public virtual Application Application { get;set; } 
        ...
    }
    

    Mapping then should look like this

    <class name="Application" >
        ...
        // class should be SQuery
        //<one-to-one name="sQuery" property-ref ="Application" class="SicQuery"/>
        <one-to-one name="sQuery" property-ref="Application" class="SQuery"/>
    
     <class name="SQuery" >
        ...
        <many-to-one name="Application" class="Application" 
                     column="Application" unique="true"/>
    

    See more here:

    5.1.2. hibernate-mapping