2

我试图用部分父复合键而不是全部引用一些子实体,为什么不能?当我使用以下映射而不是注释的映射时,就会发生这种情况。

我收到以下错误

表 VolatileEventContent 中的外键必须与表 LocationSearchView 中引用的主键具有相同的列数

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="JeanieMaster.Domain.Entities" assembly="JeanieMaster.Domain">
  <class name="LocationSearchView" table="LocationSearchView">

    <composite-id>
      <key-property name="LocationId" type="Int32"></key-property>
      <key-property name="ContentProviderId" type="Int32"></key-property>
      <key-property name="CategoryId" type="Int32"></key-property>
    </composite-id>

    <property name="CompanyName" type="String" not-null="true" update="false" insert="false"/>
    <property name="Description" type="String" not-null="true" update="false" insert="false"/>
    <property name="CategoryId" type="Int32" not-null="true" update="false" insert="false"/>
    <property name="ContentProviderId" type="Int32" not-null="true" update="false" insert="false"/>
    <property name="LocationId" type="Int32" not-null="true" update="false" insert="false"/>
    <property name="Latitude" type="Double"  update="false" insert="false" />
    <property name="Longitude" type="Double"  update="false" insert="false" />

    <bag name="Events" table="VolatileEventContent" where="DeactivatedOn IS NULL" order-by="StartDate DESC" lazy="false" cascade="none">
      <key>
        <column name="LocationId"></column>
        <column name="ContentProviderId"></column>
        <!--<column name="LocationId"></column>
        <column name="ContentProviderId"></column>
        <column name="CategoryId"></column>-->
      </key>
      <one-to-many class="Event" column="VolatileEventContentId"></one-to-many>
    </bag>

  </class>
</hibernate-mapping>

和 VolatileEventContent 映射文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="JeanieMaster.Domain.Entities" assembly="JeanieMaster.Domain">
  <class name="Event" table="VolatileEventContent" select-before-update="false" optimistic-lock="none">
    <composite-id>
      <key-property name="LocationId" type="Int32"></key-property>
      <key-property name="ContentProviderId" type="Int32"></key-property>
    </composite-id>

    <property name="Description" type="String" not-null="true" update="false" insert="false"/>

    <property name="StartDate" type="DateTime" not-null="true" update="false" insert="false" />
    <property name="EndDate" type="DateTime" not-null="true" update="false" insert="false" />

    <property name="CreatedOn" type="DateTime" not-null="true" update="false" insert="false" />
    <property name="ModifiedOn" type="DateTime" not-null="false" update="false" insert="false" />

    <many-to-one name="Location" class="Location" column="LocationId" />

    <bag name="Artistes" table="EventArtiste" lazy="false" cascade="none">
      <key name="VolatileEventContentId" />
      <many-to-many class="Artiste" column="ArtisteId" ></many-to-many>
    </bag>
  </class>
</hibernate-mapping>
4

3 回答 3

2

错误是正确的。我猜您正在使用SchemaExport基于 NHibernate 映射生成表,因为您收到的错误听起来像是在创建表和外键期间发生的。 SchemaExport将生成类似于以下的表格(请注意散布在代码中的解释):

CREATE TABLE LocationSearchView (
    LocationId int NOT NULL,
    ContentProviderId int NOT NULL,
    CategoryId int NOT NULL,

    /* ...other columns... */

    /* Note: Generated from LocationSearchView's "composite-id" element.  */
    PRIMARY KEY (LocationId, ContentProviderId, CategoryId)
);

/* Note: Table for the "Event" class. */
CREATE TABLE VolatileEventContent (
    LocationId int NOT NULL,
    ContentProviderId int NOT NULL,

    /* ...other columns... */

    /* Note: Generated from Event's "composite-id" element. */
    PRIMARY KEY (LocationId, ContentProviderId),
    /* Note: Generated from the "key" element of LocationSearchView's Events bag. */
    FOREIGN KEY (LocationId, ContentProviderId) REFERENCES LocationSearchView (LocationId, ContentProviderId)
);

...因此错误。外键必须指向完整的主键或唯一键——而不仅仅是主键的一部分。整个键是 3 列,而不是 2。为什么 NHibernate 会使用这些列作为外键?因为's bag 的<key>元素。指定子节点中的哪些列指向父节点LocationSearchViewEvents<key>

让我们考虑一下当您(或 NHibernate)尝试从这些表中进行选择时会发生什么。假设以下数据:

表位置搜索查看
LocationId ContentProviderId CategoryId
========== ============================
1 3 5
1 3 6
1 4 5
1 4 6
2 3 5
2 3 6
2 4 5
2 4 6
表 VolatileEventContent
LocationId ContentProviderId
========== ==================
1 3
1 4
2 3
2 4

"one" 不可能LocationSearchView有 "many" Event。相反,它应该是相反的。Event鉴于这些表,从to确实存在一对多的关系LocationSearchView

我不知道这个问题的正确解决方案是什么,因为我不知道你想要完成什么,但希望这有助于阐明问题到底是什么。

于 2010-03-20T15:32:05.893 回答
0

我从未使用过 nhibernate,但我猜映射与 hibernate 非常相似。

如果您不希望您的一对多(LocationSearchView 到许多 VolatileEventContent)关联使用 LocationSearchView id,则需要在包的关键元素上定义属性“property-ref”,该属性将定义要使用的属性反而 :

<bag name="Events" table="VolatileEventContent" ...>
  <key property-ref="partialId">
    <column name="LocationId"></column>
    <column name="ContentProviderId"></column>
  </key>
  <one-to-many class="Event" column="VolatileEventContentId"></one-to-many>
</bag>

(列属性在一对多标签上有效?)

现在您需要定义该名称的属性,如下所示:

<properties name="partialId" insert="false" update="false">
  <property name="LocationId" type="Int32" update="false" insert="false"/>
  <property name="ContentProviderId" type="Int32" update="false" insert="false"/>
</properties>

您已经定义了 LocationId 和 ContentProviderId。只需将这两个属性移动到属性元素内。

于 2010-03-20T11:30:38.123 回答
0

首先,您的“ LocationSearchView”映射中有一个错误,您将列定义CategoryId为属性和复合 ID 的一部分。这是错误的,但不幸的是,它在构建映射时没有被捕获,并且通常在查询对象时暴露出来。检查NHibernate 内部深处的 IndexOutOfRangeException

它可能会混淆映射解析器。我说的是令人困惑,因为在您的两个映射中,您都依赖反射和约定,这意味着更严格的编程方法:

  1. 您没有明确定义and元素的class属性,而是希望 NHibernate 从类定义本身检测类类型。如果您有任何机会在类中设置,则以下nhibernate 会期望找到已定义的类,因此需要 3 列来映射集合many-to-onebagLocationSearchViewIList<LocationSearchView> Events {get;set;}LocationSearchViewbag
  2. 您对列名称具有相同的属性名称,这使得开发更容易(它实际上只会使您更容易创建一次或两次的映射)但是如果出现错误或更改,则更难检测到哪里出了问题。

所以,让你的映射更丰富,消除我提到的错误CategoryId,还包括你的帖子中的类!

于 2010-03-26T10:11:35.273 回答