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.  [ 6 posts ] 
Author Message
 Post subject: Maintaining Foreign Key Relationships using Hibernate
PostPosted: Tue Feb 07, 2006 3:43 pm 
Newbie

Joined: Fri Jan 20, 2006 11:57 am
Posts: 4
Dear All,

I am trying to maintain a foreign key relationship using Hibernate without having the relationships in the database (This is my requirement because I dont have control on the database). In this the field which acts as a foreign key allows null values. So if I give the some number not in primary key table its inserting the record into the table, but it should not as I am maintaing the foreingh key relationship in the hbm.xml files.

I worked Emp and Dept tables without relationship in the database and deptno in Emp table allows null. Here are the hbm files for Emp and Dept

Emp.hbm.xml

<?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 Feb 7, 2006 11:44:10 PM by Hibernate Tools 3.1.0 beta3 -->
<hibernate-mapping>
<class name="test.Emp" table="Emp" schema="dbo" catalog="Policy_test">
<id name="empno" type="string">
<column name="Empno" length="50" />
<generator class="assigned" />
</id>
<many-to-one name="dept" class="test.Dept" fetch="select">
<column name="Deptno" length="50" />
</many-to-one>
<property name="ename" type="string">
<column name="Ename" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>

Dept.hbm.xml

<?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 Feb 7, 2006 11:44:10 PM by Hibernate Tools 3.1.0 beta3 -->
<hibernate-mapping>
<class name="test.Dept" table="Dept" schema="dbo" catalog="Policy_test">
<id name="deptno" type="string">
<column name="Deptno" length="50" />
<generator class="assigned" />
</id>
<property name="deptName" type="string">
<column name="DeptName" length="150" not-null="true" />
</property>
<set name="emps" inverse="true">
<key>
<column name="Deptno" length="50" not-null="true" />
</key>
<one-to-many class="test.Emp" />
</set>
</class>
</hibernate-mapping>

While inserting the record in the Emp table with the deptno not existed in Dept table its executing the follwoing sql quries internally (in the server)

Hibernate: select dept_.Deptno, dept_.DeptName as DeptName14_ from Policy_test.dbo.Dept dept_ where dept_.Deptno=?
Hibernate: insert into Policy_test.dbo.Emp (Deptno, Ename, Empno) values (?, ?, ?)
Hibernate: update Policy_test.dbo.Emp set Deptno=?, Ename=? where Empno=?


Any help regarding this is appreciated.

Thanks & Regards,
Nagesh[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 5:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Have a look at the not-found attribute of many-to-one. I'm not sure that that's exactly what you want, though. Do you want to create an Emp record with a deptno that isn't in the Dept table? The only way I can think of handling that is to maintain deptno as a property (remove the many-to-one in Emp), and use factory methods to try to load an Emp's Dept.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 10:13 pm 
Newbie

Joined: Fri Jan 20, 2006 11:57 am
Posts: 4
Thank you for your reply. My objective is if I try to insert a new Emp record with a deptno which is not existed in the dept table then I need to throw an exception and if I try to insert the emp record with deptno null or with an existing record then the record will be inserted. I dont have relationships in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 10:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That should be the default (not-found="exception"). What do your in-memory objects look like? Does the Emp object that you're expecting to cause a failed save have a Dept object? If it does, and that Dept object has an id value that doesn't exist, then what happens when the Emp object is saved is dependent on the value of the cascade attribute in your mapping file. Set cascade="none" in you mapping file; that, in combination with the default not-found="exception", should cause the behaviour you're looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 5:02 am 
Newbie

Joined: Fri Jan 20, 2006 11:57 am
Posts: 4
I tried as you said by giving cascade="none" and not-found="exception" but it did not worked. It is saving when i am giving the wrong Dept Number(whichis not in Dept table) in emp object.

Here is my modified
Emp.hbm.xml

<?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 Jan 31, 2006 5:28:18 PM by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
<class name="com.adp.es.fsa.test.Emp1" table="EMP1" schema="SCOTT">
<id name="empno" type="short">
<column name="EMPNO" precision="4" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="dept1" class="com.adp.es.fsa.test.Dept1" fetch="select" cascade="none" not-found="exception">
<column name="DEPTNO" precision="2" scale="0"/>
</many-to-one>
<property name="ename" type="string">
<column name="ENAME" length="10" />
</property>
<property name="job" type="string">
<column name="JOB" length="9" />
</property>
<property name="mgr" type="short">
<column name="MGR" precision="4" scale="0" />
</property>
<property name="hiredate" type="date">
<column name="HIREDATE" length="7" />
</property>
<property name="sal" type="big_decimal">
<column name="SAL" precision="7" />
</property>
<property name="comm" type="big_decimal">
<column name="COMM" precision="7" />
</property>
</class>
</hibernate-mapping>

Here is the way i am constructing my Emp Object

Dept1 dept1 = new Dept1((byte)30);
Emp1 emp1 = new Emp1((short)170,dept1,"Ramya","Member",(short)50,hireDate,new BigDecimal(10000),new BigDecimal(10));

In Dept table i have only 10 as DeptNumber. But here i have given 30 as DeptNumber for emp object, still emp object is getting saved into database.

Thank You
Nagesh

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 4:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think that you have proved that Hibernate doesn't do what you're hoping it does, unfortunately. If you can't add the foreign keys, you'll have to add special code to your DA layer to check that the Dept exists before saving the Emp. Bummer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.