Hey,
I'm having trouble creating the mapping of a many to one relationship.
the problem:
- every 'task' should have one 'taskState'-object
- when a task gets updated, the 'taskState'-object changes too. However, the database should keep historical 'taskState'-objects.
so, a taskState-object saves both taskId & version/sequence. The PK of 'taskState' is therefore (taskId, sequence).
a task should keep the sequence number of the last taskState, and should thus be able to work with the last version of taskState.
the code I currently have:
-task mapping file:
Code:
<?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>
<!-- Mapping Of The Task-Table -->
<class name="domain.Task" table="task">
<!-- Key Mapping -->
<id name="id"
column="task_id"
unsaved-value="null">
<generator class="native" />
</id>
<!-- Property Mapping -->
<many-to-one name="stateContainer" class="domain.TaskStateContainer"
insert="false" update="false">
<column name="task_id"/>
<column name="transaction_sequence"/>
</many-to-one>
<property name="sequenceNumber"
column="transaction_sequence"
type="integer"
/>
<!-- Mapping Of The Task's Relationships -->
<set name="inputVariables" cascade="all" inverse="true">
<key column="task_id"/>
<one-to-many class="domain.VariableAssignedIn"/>
</set>
<set name="outputVariables" cascade="all" inverse="true">
<key column="task_id"/>
<one-to-many class="domain.VariableAssignedOut"/>
</set>
</class>
</hibernate-mapping>
-taskState mapping file:
Code:
<?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>
<!-- Mapping Of The Task-State-Table -->
<class name="domain.TaskStateContainer" table="task_state">
<!-- Foreign Key & Sequence Mapping -->
<composite-id>
<key-many-to-one
name="task"
class="domain.Task"
column="task_id"
/>
<key-property
name="transactionSequence"
column="transaction_sequence"
type="integer"
/>
</composite-id>
<!-- Property Mapping -->
<property name="stateTask"
column="task_state"
type="domain.TaskState"
not-null="true"
/>
<property name="stateTransferIn"
column="task_state_transfer_in"
type="domain.TaskStateTransfer"
not-null="true"
/>
<property name="stateTransferOut"
column="task_state_transfer_out"
type="domain.TaskStateTransfer"
not-null="true"
/>
</class>
</hibernate-mapping>
the error when saving a new task:
Code:
null id in domain.Task entry (don't flush the Session after an exception occurs)
the null id error has something to do with saving the relationship. As the task is a new one, it will only receive an ID when being saved for the first time.
I tried several other ways to map this problem, but I wasn't able to find a working one.
btw: the 'insert="false" update="false"' is set because otherwise I would get following error:
Code:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: domain.Task column: task_id (should be mapped with insert="false" update="false")
any suggestions?
thanks,
Sven