我正在尝试冬眠以获得经验。我创建了一个类
Person
有两个子类:
Student
和
Worker
:
public abstract class Person {
private Long id;
...
}
public class Student extends Person { ... }
另一个班级,
Employer
,具有双向一对多关系
工人
.
public class Worker extends Person {
private Employer employer;
...
}
public class Employer {
private String taxId;
private Set<Worker> employees = new HashSet<Worker>();
...
}
对其映射
<class name="Employer" table="EMPLOYER">
<id name="taxId" column="TAX_ID" length="11">
<generator class="assigned"/>
</id>
...
<set name="employees" inverse="true">
<key column="EMPLOYER_TAX_ID"/>
<one-to-many class="Worker"/>
</set>
</class>
继承层次结构采用混合策略建模,其中
学生
映射到
PERSON
桌子,但是
工人
存储在自己的表中,并与一个外键联接:
<class name="Person" table="PERSON">
<id name="id" column="PERSON_ID" type="long" unsaved-value="0">
<generator class="native"/>
</id>
<discriminator column="PERSON_TYPE" type="string"/>
...
<subclass name="Student" discriminator-value="STU"> ... </subclass>
<subclass name="Worker" discriminator-value="WRK">
<join table="WORKER">
<key column="WORKER_ID"/>
<many-to-one name="employer" column="EMPLOYER_TAX_ID" cascade="save-update"/>
...
</join>
</subclass>
</class>
我使用ApacheDerby 10.5.3.0并通过设置
hibernate.hbm2ddl.auto
到
create-drop
.
为了测试所有这些,我使用以下数据集创建了一个DBUnit测试:
<EMPLOYER TAX_ID = "1234567890"
...
/>
<PERSON PERSON_ID = "12345"
PERSON_TYPE = "WRK"
...
/>
<WORKER WORKER_ID = "12345"
EMPLOYER_TAX_ID = "1234567890"
...
/>
我有一个测试,它加载工人实体并验证它是否拥有正确的员工。这是通行证。然后对相反方向进行测试:
String taxId = "1234567890";
Employer employer = (Employer) session.get(Employer.class, taxId);
assertNotNull(employer);
assertThat(employer.getEmployees().size(), is(1));
执行时,最后一个断言失败,因为雇员集为空。
深入挖掘,我发现出于某种原因
Hibernate在表Person中查找(并创建)Employer_Tax_ID列,而不是Worker
!它也存在于worker中,但在查询中不使用它。用于填充雇员集的select语句是:
select
employees0_.EMPLOYER_TAX_ID as EMPLOYER10_1_,
employees0_.PERSON_ID as PERSON1_1_,
employees0_.PERSON_ID as PERSON1_1_0_,
employees0_.FIRST_NAME as FIRST3_1_0_,
employees0_.FAMILY_NAME as FAMILY4_1_0_,
employees0_.DATE_OF_BIRTH as DATE5_1_0_,
employees0_.HOME_ADDRESS as HOME6_1_0_,
employees0_.CITY as CITY1_0_,
employees0_.ZIP as ZIP1_0_,
employees0_1_.EMPLOYER_TAX_ID as EMPLOYER2_2_0_,
employees0_1_.JOB_TITLE as JOB3_2_0_,
employees0_1_.JOB_GRADE as JOB4_2_0_,
employees0_1_.START_DATE as START5_2_0_
from
PERSON employees0_
inner join
WORKER employees0_1_
on employees0_.PERSON_ID=employees0_1_.WORKER_ID
where
employees0_.EMPLOYER_TAX_ID=?
这是为什么?和
我怎样才能让Hibernate在工人桌上找到雇主税务ID?
注意,由于这是一个实验项目,我可以改变任何东西。我很感激任何解决方法,但我更希望了解正在发生的事情并尽可能地修复这个映射。
更新:
如果我换成清洁的
<joined-subclass>
继承映射策略,生成的模式看起来应该,测试通过。这是一个足够好的解决方法,但我仍然好奇是否有一种方法可以使混合策略正常工作。