Hibernate displays a multi-one composite key

I am trying to display a complex key with many relationships. Here is my SQL Server schema with credential fields.

The following are mapping files:

In the agency 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 alert status 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 get 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!

+4
source share
1 answer

Your mapping file for AgencyLoginAlertState refers to an invalid table. It says:

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

Since both classes get mapped to the same table, Hibernate mapper is confronted with a primary key definition for this table. Based on your database schema, this line should be:

 <class name="AgencyLoginAlertState" table="AGENCY_ALERT_STATE"> 
+1
source

Source: https://habr.com/ru/post/1441652/


All Articles