NHibernate复合ID映射问题,双向/单向OneToMany,ManyToOne和ManyToMany [英] NHibernate composite id mapping issue, bi/uni-directional OneToMany, ManyToOne and ManyToMany

查看:85
本文介绍了NHibernate复合ID映射问题,双向/单向OneToMany,ManyToOne和ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,所有实体都具有复合主键[systemId为long,deviceId为long,id为long].在保存实体之前,需要手动填充值. 我正在使用代码优先"的方法,并提供了简单引用NHibernate.Mapping.Attributes扩展来定义具有属性的架构,就像在基于Java的Hibernate中一样.

In my project all of entities have composite primary keys [systemId as long, deviceId as long, id as long]. Values filled manualy before I save the entity. I'm using "code first" approach and to provide simplicity references NHibernate.Mapping.Attributes extension to define schema with attributes just like in Java based Hibernate.

所有实体都有一个抽象的基本类型,该基本类型提供了共享的属性和功能:

All entities have an abstract base type which provides shared properties and functionality:

[Serializable]
public abstract class EntityBase
{

    [CompositeId(0, Name = "id", ClassType = typeof(EntityId))]
    [KeyProperty(1, Name = "systemId", Column = "restId")]
    [KeyProperty(2, Name = "deviceId", Column = "deviceId")]
    [KeyProperty(3, Name = "id", Column = "id")]
    private EntityId id = new EntityId(); // this is a component, see below

    [Property(Column = "isDeleted", NotNull = true)]
    private bool deleted = false;

    public EntityId Id 
    { 
        get { return id; }
        set { id = value; }
    }

    public bool Deleted 
    {
        get { return deleted; }
        set { deleted = value; }
    }

}

复合ID后面有一个组件,代表复杂的主键:

Behind the composite id there is a component, which represent the complex primary key:

[Serializable]
[Component]
public class EntityId
{

    [Property(Column = "restId", NotNull = true)]
    private long systemId = 0;

    [Property(NotNull = true)]
    private long deviceId = 0;

    [Property(NotNull = true)]
    private long id = 0;

    public long SystemId 
    {
        get { return systemId; }
        set { systemId = value; } 
    }

    public long DeviceId 
    {
        get { return deviceId; }
        set { deviceId = value; } 
    }

    public long Id 
    {
        get { return id; }
        set { id = value; } 
    }

}

我定义了两个名为OTMList和OTMItem的实体,它们相互之间具有双向的OneToMany和ManyToOne关联.

I definied two entities called OTMList and OTMItem which have bi-directional OneToMany and ManyToOne associations to each other.

[Serializable]
[Class]
public class OTMList : EntityBase
{

    [List(0, Cascade = "none", Generic = true, Lazy = CollectionLazy.True)]
    [Key(1, Column = "id")]
    [Index(2, Column = "id")]
    [OneToMany(3, NotFound = NotFoundMode.Exception, ClassType = typeof(OTMItem))]
    private IList<OTMItem> otmItems = new List<OTMItem>();

    public IList<OTMItem> OTMItems
    {
        get { return otmItems; }
        set { otmItems = value; }
    }

}

[Serializable]
[Class]
public class OTMItem : EntityBase
{

    [ManyToOne(0, Name = "otmList", ClassType = typeof(OTMList), Column = "OTMListId", Cascade = "none", Lazy = Laziness.Proxy)]
    private OTMList otmList = null;

    public OTMList OTMList
    {
        get { return otmList; }
        set { otmList = value; }
    }

}

休眠映射xml文件包含以下信息:

The hibernate mapping xml file contains the following information:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping default-access="field" auto-import="true" assembly="NHibernateTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateTest.ListTest.OTM.OTMList, NHibernateTest">
    <composite-id class="NHibernateTest.Domain.EntityId, NHibernateTest" name="id">
      <key-property name="systemId" column="restId" />
      <key-property name="deviceId" column="deviceId" />
      <key-property name="id" column="id" />
    </composite-id>
    <property name="deleted" column="isDeleted" not-null="true" />
    <list name="otmItems" lazy="true" cascade="none" generic="true">
      <key column="id" />
      <index column="id" />
      <one-to-many class="NHibernateTest.ListTest.OTM.OTMItem, NHibernateTest" not-found="exception" />
    </list>
  </class>
  <class name="NHibernateTest.ListTest.OTM.OTMItem, NHibernateTest">
    <composite-id class="NHibernateTest.Domain.EntityId, NHibernateTest" name="id">
      <key-property name="systemId" column="restId" />
      <key-property name="deviceId" column="deviceId" />
      <key-property name="id" column="id" />
    </composite-id>
    <property name="deleted" column="isDeleted" not-null="true" />
    <many-to-one name="otmList" class="NHibernateTest.ListTest.OTM.OTMList, NHibernateTest" column="OTMListId" cascade="none" lazy="proxy" />
  </class>
</hibernate-mapping>

当我使用NHibernate的SchemaValidator验证架构时,出现以下异常:

When I validate the schema with SchemaValidator of the NHibernate I get the following exception:

Foreign key (FKF208BF0B9A2FCB3:OTMItem [OTMListId])) must have same number of columns as the referenced primary key (OTMList [restId, deviceId, id])

当我尝试创建单向ManyToOne或双向/单向ManyToMany关联时,问题也相同.

The problem are the same too when I try to create uni-directional ManyToOne or bi/uni-directional ManyToMany associations.

有人可以帮我吗?

此处提供完整的源代码:

The full source code available here:

NHibernateTest源代码

推荐答案

问题已解决,请查看以下内容:

The problem is solved, check this out:

http://jzo001.wordpress.com/category/nhibernate/

这篇关于NHibernate复合ID映射问题,双向/单向OneToMany,ManyToOne和ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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