-->
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.  [ 8 posts ] 
Author Message
 Post subject: Inserting two Objects with FK ; one-to-many instead -to-one
PostPosted: Mon Apr 23, 2007 12:18 pm 
Regular
Regular

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

i"ve a problem with a type of query I have to write.

I have two tables "Item" and "Molecule". There is a one-to-one relationship between them. I wrote by now a query, which inserts the "Item"-object and the databse generates a unique primary key. Now I want to insert a "Molecule"-object which needs the primary key of the "Item"-object as foreign key.

1. How can I get the primary key from my "Item"-object? Problem: It just gets created in the moment I commit the transaction.

2. Why do I get a one-to-many relationship in the mapping file, when there is a one-to-one relationship. (I designed the database schema with dbdesigner 4.)

So, if someone can help me, especially with the first problem, that would be great.

Greetz Carina


Hibernate version: 3.2 beta 8

Create Statement of the tables:
Code:
CREATE TABLE item (
  id INTEGER  NOT NULL ,
  type  VARCHAR(45) NULL,
  PRIMARY KEY(id)
);

CREATE TABLE molecules (
  item_id INTEGER  NOT NULL,
  name VARCHAR(100) NULL,
  smiles VARCHAR(100) NULL,
  iupac VARCHAR(100) NULL,
  inchi VARCHAR(100) NULL,
  formula VARCHAR(100) NULL,
  createBy VARCHAR(100) NULL,
  nofAtoms INT NULL,
  nofBonds INT NULL,
  nofHeavyAtoms INT NULL,
  nofHeavyBonds INT NULL,
  nofFragments INT NULL,
  nofSuperAtoms INT NULL,
  nofRings INT NULL,
  nofRGroups INT NULL,
  medianBondLength DOUBLE PRECISION NULL,
  mass DOUBLE PRECISION NULL,
  directory VARCHAR(100) NULL,
  workflowID INTEGER  NULL,
  PRIMARY KEY(item_id),
  FOREIGN KEY(item_id)
    REFERENCES item(id)
);

CREATE SEQUENCE item_seq
start with 1
increment by 1
nomaxvalue;

CREATE TRIGGER item_trigger
before insert on item
for each row
begin
SELECT item_seq.nextval into :new.id from dual;
end;
/


Code between sessionFactory.openSession() and session.close():
Code:
Transaction tx = null;
       
       // Item Object
       try {        
          tx = session.beginTransaction();
       
          Item itemObj = new Item();
          itemObj.setType("MoleculeGraph");
                 
          session.save(itemObj);
          
          tx.commit();
       }
       catch(Exception e) {
          tx.rollback();
          e.fillInStackTrace();
          logger.debug(e.getStackTrace());
       }
       
       // Molecules Object
       try {        
          tx = session.beginTransaction();
                 
          session.load(Item.class, arg1)
          Molecules molObj = this.castToMolecules();
          
          molObj.setItemId(2);
          
          int molObjId = (Integer) session.save(molObj);
          
          tx.commit();
       }
       catch(Exception e) {
          tx.rollback();
          logger.debug(e.getStackTrace());
          throw e;
       }


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">
<!-- Generated Apr 23, 2007 4:53:05 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
    <class name="de.fhg.scai.bio.csr.io.database.hibernate.Item" table="ITEM">
        <id name="id" type="int">
            <column name="ID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="type" type="string">
            <column name="TYPE" length="45" />
        </property>
        <set name="moleculeses" inverse="true">
            <key>
                <column name="ITEM_ID" precision="22" scale="0" not-null="true" unique="true" />
            </key>
            <one-to-many class="de.fhg.scai.bio.csr.io.database.hibernate.Molecules" />
        </set>
    </class>
</hibernate-mapping>


Name and version of the database you are using: Oracle 10g


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 12:56 pm 
Newbie

Joined: Fri Apr 20, 2007 3:46 pm
Posts: 15
Hey Carina,

1. How can I get the primary key from my "Item"-object? Problem: It just gets created in the moment I commit the transaction.

You could try the following

<class name="de.fhg.scai.bio.csr.io.database.hibernate.Item" table="ITEM">
<id name="id" type="int">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">item_seq</param>
</generator>
</id>


This will generate your ids based on the sequence and you should be able to get your ids.

2. I am not sure how you generate the mapping file but you can modify the mapping file yourself to have one-to-one

<hibernate-mapping>
<class name="de.fhg.scai.bio.csr.io.database.hibernate.Item" table="ITEM">
<id name="id" type="int">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">item_seq</param>
</generator>
</id>
<property name="type" type="string">
<column name="TYPE" length="45" />
</property>
<one-to-one name="moleculeses" class="de.fhg.scai.bio.csr.io.database.hibernate.Molecules"/>
</class>

<class name="de.fhg.scai.bio.csr.io.database.hibernate.Molecules" table="molecules">
<id name="id" column="item_id">
<generator class="foreign">
<param name="property">item</param>
</generator>
</id>
<one-to-one name="item"
class="de.fhg.scai.bio.csr.io.database.hibernate.Item
constrained="true"/>
</class>
</hibernate-mapping>


This is essentially what you want. One-to-one mapping where tables are connected by their primary keys. Also notice that Molecules table will get its primary key value from the primary key on ITEM table. The primary key on ITEM table is generated by your sequence.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 1:12 pm 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
bumbalo wrote:
2. I am not sure how you generate the mapping file but you can modify the mapping file yourself to have one-to-one


I know that I can modify it myself, but the problem is that I have about 20 or 30 of these failures and to change everything by hand is not the best way for my program.
But you're right, I forget to write how I generate the mapping files. I use Hibernate Tools and I think I'll make a post for this issue in the Tools forum.

I'll trie you're idea to my first problem as soon as possible, but one question I have: In which file do I have to write your code? In the mapping file, I think!?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 1:30 pm 
Newbie

Joined: Fri Apr 20, 2007 3:46 pm
Posts: 15
Yes you can put it all one in mapping file config.hbm.xml or you can put the mappings into seperate *.hbm.xml files. I always generated mapping files by hand so I am not familiar with Hibernate Tools.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 1:40 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
A one-to-one in Hibernate uses shared primary keys. For the related entity you'd need to specify the foreign id generator.

http://www.hibernate.org/hib_docs/v3/re ... -generator

If you're using a synthetic primary key on the related entity with a FK pointing to the first entity (looks like this is what you've got), that's mapped as a many-to-one. If you want one-to-one behavior in that case, you can use unique="true".

Hope this helps.

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 9:05 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
@bumbalo: I just tried your answer and it works perfect. Of course, I had to remove the trigger which I had implemented in the database, but now I get the id back from the session.save function. Thanks a lot! Now I just have to get put, how I can generate this part of the xml code automatically with hibernate tools.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:28 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
cbredesen wrote:
A one-to-one in Hibernate uses shared primary keys. For the related entity you'd need to specify the foreign id generator.

http://www.hibernate.org/hib_docs/v3/re ... -generator

If you're using a synthetic primary key on the related entity with a FK pointing to the first entity (looks like this is what you've got), that's mapped as a many-to-one. If you want one-to-one behavior in that case, you can use unique="true".

Hope this helps.

-Chris


At first I want to thank you for your help but I have to say that I don"t really get what you want to say to me. I generate my files with hibernate tools and want that they are directly created as one-to-one mappings. When you think you can help me, please answer this post: http://forum.hibernate.org/viewtopic.php?t=973656.
Thx! Carina


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:30 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Quote:
At first I want to thank you for your help but I have to say that I don"t really get what you want to say to me. I generate my files with hibernate tools and want that they are directly created as one-to-one mappings. When you think you can help me, please answer this post: http://forum.hibernate.org/viewtopic.php?t=973656.


Sorry about that. Something went awry with "View Unanswered Posts" and I replied to the wrong post, sorta. Going there now.

-Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.