Hibernate映射多对一复合键

时间:2012-10-23 20:31:31

标签: hibernate hibernate-mapping

我正在尝试使用多对一关系映射复合键。 Here is my SQL Server schema, with identity id fields.

以下是我的映射文件:

对于代理商登录提醒表:

<hibernate-mapping>
<class name="AgencyLoginAlert" table="AGENCY_ALERTS">
    <id name="agencyAlertsGID" type="integer">
        <column name="AGENCY_ALERTS_GID" />
        <generator class="identity" />
    </id>
    <property name="createdByName" type="string">
        <column name="CREATED_BY_NAME" length="30" />
    </property>
    <property name="creationDatetime" type="date">
        <column name="CREATION_DATETIME" />
    </property>
    <property name="lastUpdatedByName" type="string">
        <column name="LAST_UPDATED_BY_NAME" length="30" />
    </property>
    <property name="lastUpdatedDatetime" type="date">
        <column name="LAST_UPDATED_DATETIME" />
    </property>
    <property name="startDisplayDatetime" type="date">
        <column name="START_DISPLAY_DATETIME" />
    </property>
    <property name="endDisplayDatetime" type="date">
        <column name="END_DISPLAY_DATETIME" />
    </property>
    <property name="messageCategoryCode" type="string">
        <column name="MESSAGE_CATEGORY_CODE" length="6" />
    </property>
    <property name="messageTitle" type="string">
        <column name="MESSAGE_TITLE" length="250" />
    </property>
    <property name="messageStatusCode" type="string">
        <column name="MESSAGE_STATUS_CODE" length="10" />
    </property>
    <property name="messageBodyText" type="string">
        <column name="MESSAGE_BODY_TEXT" length="2048" />
    </property>

    <set name="agencyLoginAlertState" table="AGENCY_ALERT_STATE" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="AGENCY_ALERTS_GID" not-null="true" />
        </key>
        <one-to-many class="AgencyLoginAlertState" />
    </set>
</class>

对于代理商登录警报状态表:

<hibernate-mapping>
<class name="AgencyLoginAlertState" table="AGENCY_ALERTS">
    <composite-id>
          <key-many-to-one name="agencyAlertsGID" class="AgencyLoginAlert">
                <column name="AGENCY_ALERTS_GID" />
          </key-many-to-one>
          <key-property name="stateCode" type="string">
              <column name="STATE_CODE" length="2" />
        </key-property>
    </composite-id>
    <many-to-one name="agencyLoginAlert" class="AgencyLoginAlert" fetch="select" insert="false" update="false">
        <column name="AGENCY_ALERTS_GID" not-null="true" />
    </many-to-one>
</class>

我收到以下错误:

 org.hibernate.MappingException: Foreign key (FK416A6411D6548611:AGENCY_ALERTS
 [AGENCY_ALERTS_GID])) must have same number of columns as the referenced primary key 
(AGENCY_ALERTS [AGENCY_ALERTS_GID,STATE_CODE])

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

AgencyLoginAlertState的映射文件引用了错误的表。它说:

<class name="AgencyLoginAlertState" table="AGENCY_ALERTS">

由于两个类都被映射到同一个表,因此Hibernate的映射器会碰撞该表的主键定义。根据您的数据库架构,该行应为:

<class name="AgencyLoginAlertState" table="AGENCY_ALERT_STATE">