-->
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.  [ 1 post ] 
Author Message
 Post subject: using foreign key in multi level inheritance
PostPosted: Wed Jun 06, 2007 11:10 am 
Newbie

Joined: Wed Jun 06, 2007 9:29 am
Posts: 1
Hi,

class Person { ... }
class Employee extends Person { ... }
class Manager extends Employee { ... }

I'm trying to get hibernate to work with my inheritance Hierarchy of Person, Employee, Manager. I have a table per subclass setup in my mapping file. This is fine as long as I use hibernate exclusively to access data in the database. However with the current configuration if someone were to add a record directly on the database bypassing hibernate then it would be possible to create an entry in the PERSON table with a corresponding entry in the MANAGER but not in the EMPLOYEE table which can be a problem.

What I would like to have is a foreign key on the Manager table mapped to a primary key on the Employee table so that there is some constraints on the database. Currenly if I have an foreign key column (eg. FK_EMPLOYEE_ID) on the Manager table, hibernate will not add an entry in this field when doing an insert. Does anyone know of a work around in hibernate for this?

Below is an example of the database schema I would like hibernate to work with.

Table "PERSON"
Column | Type |
----------+-----------+
PERSON_ID | integer |
NAME | text |
DOB | timestamp |

Table "EMPLOYEE"
Column | Type |
-----------------+---------+
EMPLOYEE_ID | integer |
FK_PERSON_ID | integer |
HIRE_DATE | timestamp |
FK_DEPARTMENT_ID | integer |


Table "MANAGER"
Column | Type |
---------------------+---------+
MANAGER_ID | integer |
FK_EMPLOYEE_ID | integer |
FK_PERSON_ID | integer |
NUMBER_OF_SUBS | integer |


additional here is my mapping file


<class
name="example.Person"
table="PERSON" lazy="false">

<id name="id" type="int" column="PERSON_ID">
<generator class="sequence">
<param name="sequence">PERSON_SEQ</param>
</generator>
</id>

<property name="name" column="NAME" type="java.lang.String"
not-null="true" length="-1" />

<property name="dateOfBirth" column="DOB"
type="java.util.Date" not-null="true" />

<joined-subclass
name="example.Employee"
extends="example.Person"
table="EMPLOYEE">

<key column="FK_PERSON_ID"/>

<property name="hireDate" column="HIRE_DATE"
type="java.util.Date" not-null="true" />

<many-to-one
name="department"
class="example.Department"
not-null="true"
cascade="none"
fetch="select"
>
<column name="FK_DEPARTMENT_ID"/>
</many-to-one>

<joined-subclass
name="example.Manager"
extends="example.Employee"
table="MANAGER">

<key column="FK_PERSON_ID"/>

<property name="numberOfSubordinates" column="NUMBER_OF_SUBS"
type="java.lang.Integer" not-null="false" />

</joined-subclass>

</joined-subclass>

</class>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.