-->
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: Many-to-one instead of one-to-one
PostPosted: Mon Apr 23, 2007 1:16 pm 
Regular
Regular

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

I've a problem and perhaps someone can help me.

I have two tables "Item" and "Molecule". There is a one-to-one relationship between them, but I get a many-to-one relationship created in the mapping file. Why?
(I designed the database schema with dbdesigner 4.)

So, if someone can help me that would be great.

Greetz Carina


Hibernate version: 3.2 beta 8

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

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;
/


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>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 12:48 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Foreign key relationships in the database are modeled in hibernate as many-to-one. This is because relationally, a foreign key does not determine multiplicity. A foreign key on the related table means only that there is a relation; not that there is a unique relation. A FK defines only "-to-one", not "one" or "many".

So if you use one-to-one in Hibernate, you need to implement this with shared primary keys. Is suspect you don't want this, so what you need is a many-to-one with unique="true". This generates a unique constraint on the foreign key so the many-to-one has one-to-one multiplicity.

This is discussed thoroughly in Java Persistence with Hibernate :)

Hope this helps,

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:53 am 
Regular
Regular

Joined: Thu Apr 05, 2007 7:05 am
Posts: 53
Location: Troisdorf, Germany
So you say that I already have what I want to, am I right?!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 8:13 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
If what you intended was a FK relationship, then yes! :)

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 10:16 am 
Regular
Regular

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 4:34 pm 
Newbie

Joined: Thu May 03, 2007 4:20 pm
Posts: 2
Hi
I have the same table structure.
If Item and Moleculeses were the VO and need to do a create in both tables.



How will the setMoleculeses method look?

public void setMoleculeses (Set moleculeses ) {
this.moleculeses = moleculeses ;

//Should this be opened and item set to this?

Iterator iterator =this.moleculeses .iterator();
if(iterator.hasNext()){

((Moleculeses )iterator.next()).setItem(this);
}



}
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 4:40 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
I would make getMolecules/setMolecules protected so that only Hibernate uses them (or switch to field access). Then, write methods that are more like what you need:

Code:
/**
* Add a molecule. Not used by Hibernate.
*/
public void addMolecule(Molecule m) {
  getMolecules().add(m);
  m.setItem(this);
}


and so on.

This keeps the domain model tight and works very well with Hibernate.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:40 pm 
Newbie

Joined: Thu May 03, 2007 4:20 pm
Posts: 2
Thank you!


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.