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