Nhibernate 3中的一对一映射 [英] one-to-one mapping in Nhibernate 3

查看:173
本文介绍了Nhibernate 3中的一对一映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

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; }
    }
}

SQuery和Application之间的关系是SQuery中名为"Application"的FK

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

这些是de hbm文件

these are de hbm files

Application.hbm.xml

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

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>

问题是,当我尝试访问一些SQuery对象的列表时,出现以下异常:

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>();

预先感谢

推荐答案

one-to-one 的点是引用关系.这种映射不能与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; } 
    ...
}

映射应如下图所示

<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"/>

在此处查看更多信息:

这篇关于Nhibernate 3中的一对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆