Reputation: 178
I am trying to map a composite key with a many to one relationship. Here is my SQL Server schema, with identity id fields.
Below are my mapping files:
For the Agency Login Alert table:
<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>
For the Agency Login Alert State table:
<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>
I am getting the following error:
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])
Any suggestions? Thanks!
Upvotes: 4
Views: 5315
Reputation: 6901
Your mapping file for AgencyLoginAlertState
is referencing the wrong table. It says:
<class name="AgencyLoginAlertState" table="AGENCY_ALERTS">
Since both classes are getting mapped to the same table, Hibernate's mapper is colliding over the definition of the primary key for that table. Based on your database schema, that line should be:
<class name="AgencyLoginAlertState" table="AGENCY_ALERT_STATE">
Upvotes: 1