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]
|