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