Hi everyone,
I have three tables: run, workflow and run_starts_workflow. Run and Workflow consist each a primary key and some attributes. The table run_starts_workflow has just two foreign-key-relationships to the other two tables. Between run and run_starts_workflow it is a many-to-one realtion and between workflow and run_starts_workflow it is a one-to-one relation.
My problem is, that when I save the run object and the workflow object I don't find a way to connect them to each other. Both have the functions setWorkflows(Set s) / setRuns(Set s) but when I get the objects out of the database there is no connection between them. How can I solve this?
Carina
PS: I know that I could remove the run_starts_workflow table but my boss wants this...
PPS: Below my code how I tried to solve the problem.
Hibernate version: 3.2 beta 8
Mapping documents:
Workflow
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">
<!-- Generated May 10, 2007 10:20:11 AM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="de.fhg.scai.bio.csr.io.database.hibernate.DatabaseWorkflow" table="WORKFLOW">
<id name="id" type="int">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">workflow_seq</param>
</generator>
</id>
<property name="rundate" type="date">
<column name="RUNDATE" length="7" />
</property>
<property name="starttime" type="int">
<column name="STARTTIME" precision="22" scale="0" />
</property>
<set name="runs" inverse="true" table="RUN_STARTS_WORKFLOW">
<key>
<column name="WORKFLOW_ID" precision="22" scale="0" not-null="true" />
</key>
<many-to-many entity-name="de.fhg.scai.bio.csr.io.database.hibernate.DatabaseRun">
<column name="RUN_RUNID" precision="22" scale="0" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
RunCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 10, 2007 10:20:11 AM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="de.fhg.scai.bio.csr.io.database.hibernate.DatabaseRun" table="RUN">
<id name="runid" type="int">
<column name="RUNID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="runname" type="string">
<column name="RUNNAME" length="100" />
</property>
<set name="workflows" inverse="true" table="RUN_STARTS_WORKFLOW">
<key>
<column name="RUN_RUNID" precision="22" scale="0" not-null="true" />
</key>
<many-to-many entity-name="de.fhg.scai.bio.csr.io.database.hibernate.DatabaseWorkflow">
<column name="WORKFLOW_ID" precision="22" scale="0" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
DatabaseWorkflow dbWorkflow = new DatabaseWorkflow();
int dbWorkflowId = (Integer) session.save(dbWorkflow);
// load run and add this workflow
DatabaseRun dbRun = this.loadRunByID(Workflow.getInstance().getRun());
if (dbRun == null) {
Set workflows = new TreeSet();
workflows.add(dbWorkflow);
dbRun = new DatabaseRun();
dbRun.setRunid(wf.getInstance().getRun());
dbRun.setWorkflows(workflows);
session.save(dbRun);
} else {
dbRun.getWorkflows().add(dbWorkflow);
session.update(dbRun);
}
Set runs = new TreeSet();
runs.add(dbRun);
dbWorkflow.setRuns(runs);
session.update(dbWorkflow);
Name and version of the database you are using: Oracle 10g