gpt4 book ai didi

java - Hibernate:这个映射到外键连接的子类有什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:30:49 26 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 来获得经验。我创建了一个包含两个子类的类 Person:StudentWorker:

public abstract class Person {
private Long id;
...
}

public class Student extends Person { ... }

另一个类 EmployerWorker 具有双向一对多关系。

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>

继承层次结构采用混合策略建模,其中 Student 映射到 PERSON 表,但 Worker 存储在自己的表中,通过外键连接:

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

我使用 Apache Derby 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 而不是 WORKER 中查找(并创建)EMPLOYER_TAX_ID 列!它也存在于 WORKER 中,但查询中未使用它。用于填充员工集合的选择语句是:

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 在 WORKER 表中找到 EMPLOYER_TAX_ID?

请注意,由于这是一个实验项目,我几乎可以更改任何内容。我感谢任何解决方法,但我更愿意了解正在发生的事情并按原样修复此映射(尽可能多)。

更新:如果我切换到一个干净的 <joined-subclass> 继承映射策略,生成的模式看起来应该是正确的,并且测试通过了。这是一个足够好的解决方法,但我仍然很好奇是否有办法使混合策略正常工作。

最佳答案

这听起来像是已知错误:http://opensource.atlassian.com/projects/hibernate/browse/HHH-1015

它自古以来就广为人知,并已被报道过无数次。尽管如此,他们仍未修复它...

关于java - Hibernate:这个映射到外键连接的子类有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2621733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com