-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Many-to-one relation with a third table between
PostPosted: Thu May 10, 2007 4:42 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
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>


Run
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.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


Last edited by N-te Jr. on Mon May 14, 2007 6:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Resolution?
PostPosted: Thu May 10, 2007 11:00 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
Hi Carina,

If you get a resolution to this please post it. I'm having a similar problem where hibernate will insert rows into the outer tables but not the table in the middle that is linking the two. I've been scratching my head for a while on this one without much success.

Pneu


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 13, 2007 2:36 pm 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
Hi Pneu,

until now I don't have a solution. But I hope that someone soon will help us.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.