Hello to all. I have a problem with the following construct, when there is a self-reference in the PROGRAMMER table. The test case that I run throws an exception, that shows that the program tries to find the MANAGER_ID column in the wrong table (EMPLOYEE) . As you can see, I tried to change the TABLE attribute, but it does not help, so I simply put there a "DONT_CARE" value to emphasize the problem.
I use Hibernate 3.2.6, Oracle 10g, Java 1.4.
Please help me, as I need it urgent. Thank you.
Code:
Employee.sql
create table EMPLOYEE
(
ID number(15) not null,
name varchar2(20) not null,
TYPE_DISCRIMINATOR varchar2(20) not null
);
alter table EMPLOYEE
add constraint e_id primary key (ID);
create table PROGRAMMER
(
ID number(15) not null,
MANAGER_ID number(15),
ROOM number(15) not null
);
alter table PROGRAMMER
add constraint P_ID primary key (ID)
using index;
alter table PROGRAMMER
add constraint E_ID_FK foreign key (ID)
references EMPLOYEE (ID);
alter table PROGRAMMER
add constraint M_ID_FK foreign key (MANAGER_ID)
references PROGRAMMER (ID);
insert into employee (id, name, type_discriminator)
values (1, 'a', 'Manager');
insert into employee (id, name, type_discriminator)
values (2, 'b', 'Programmer');
insert into employee (id, name, type_discriminator)
values (3, 'c', 'Programmer');
insert into programmer (id, manager_id, room)
values (1, NULL, 1);
insert into programmer (id, manager_id, room)
values (2, 1, 1);
insert into programmer (id, manager_id, room)
values (3, 1, 2);
commit;
Code:
Employee.java
public abstract class Employee{
private String name;
private Integer id;
public Integer getId(){
return id;}
public void setId(Integer id){
this.id = id;}
public String getName(){
return name;}
public void setName(String name){
this.name = name;}
public Employee(){}
}
Code:
ProgEmpl.java
import java.util.HashSet;
import java.util.Set;
public class ProgEmpl extends Employee{
private Set programmers;
private Integer room;
public Integer getRoom(){
return room;}
public void setRoom(Integer room){
this.room = room;}
public Set getProgrammers(){
return programmers;}
public void setProgrammers(Set programmers){
this.programmers = programmers;}
public ProgEmpl(){
super();
programmers = new HashSet();}
}
Code:
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE" lazy="false">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="assigned">
</generator>
</id>
<discriminator column="TYPE_DISCRIMINATOR" type="string" length="20" />
<property name="name" type="java.lang.String" update="true"
insert="true" column="NAME" length="20" />
<subclass name="ProgEmpl" discriminator-value="ProgEmpl">
<set name="programmers" table="DONT_CARE" lazy="false"
inverse="false" cascade="none" sort="unsorted">
<key column="MANAGER_ID"></key>
<one-to-many class="ProgEmpl" />
</set>
<join table="PROGRAMMER" fetch="select">
<key column="ID"></key>
<property name="room" type="java.lang.Integer"
column="ROOM" length="15" />
</join>
</subclass>
</class>
</hibernate-mapping>