Hi all. I'm getting my feet wet with Hibernate and have a tricky legacy database mapping that I need some help with.
I have two tables in a Parent/Children like relationship: a "registration" (think of this as a representation of the current state of a registration) and an associated "history" of the states that a registration has gone through. The problem seems to be that the primary key in the Registration table is a composite id, and the primary key in the RegistrationHistory table is a sequence generated id (i.e. single column) to allow for multiple history entries for a given registration.
Assume the tables are defined as follows:
Code:
Registration
------------------
identifier (PK)
source (PK)
current_state
RegistrationHistory
----------------------
history_id (PK)
identifier (FK)
source (FK)
old_state
new_state
I have tried several mappings with no luck. Here's my current incarnation:
Code:
Registration
--------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="RegistrationDTO" table="Registration">
<composite-id>
<key-property name="identifier" column="identifier"/>
<key-property name="source" column="source"/>
</composite-id>
<property name="currentState" column="current_state"/>
<!-- registration history set mapping-->
<set name="registrationHistory" cascade="all" lazy="true" order-by="history_id asc">
<key>
<column name="identifier" not-null="true"/>
<column name="source" not-null="true"/>
</key>
<one-to-many class="RegistrationHistoryDTO"/>
</set>
</class>
</hibernate-mapping>
RegistrationHistory
----------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="RegistrationHistoryDTO" table="RegistrationHistory">
<id name="historyID" column="history_id">
<generator class="sequence">
<param name="sequence">seq_hist_id</param>
</generator>
</id>
<property name="identifier" column="identifier" not-null="true"/>
<property name="source" column="data_source_id" not-null="true"/>
<property name="oldState" column="old_state"/>
<property name="newState" column="new_state"/>
</class>
</hibernate-mapping>
I get the following mapping exception:
org.hibernate.MappingException: Repeated column in mapping for entity: RegistrationDTO column: identifier (should be mapped with insert="false" update="false")
I want each RegistrationDTO to have a property that is Set of RegistrationHistoryDTO objects (i.e. a one-to-many relationship). It seems to be indicating that I'm mapping the same column twice. But in all of the documentation examples, this is done repeatedly for simple single column ids. When you try to do this with composite-ids Hibernate seems to have a problem with it.
Obviously I'm doing something wrong. Any help/clues would be greatly appreciated. Thanks in advance!