Hibernate:映射到外键连接的子类有什么问题?
我正在尝试使用 Hibernate 来获得经验。我创建了一个类 Person
,它有两个子类:Student
和 Worker
:
public abstract class Person {
private Long id;
...
}
public class Student extends Person { ... }
另一个类 Employer
有一个双向子类与 Worker 的一对多关系。
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 中的 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 在 WORKER 表中找到 EMPLOYER_TAX_ID?
请注意,由于这是一个实验项目,因此我可以更改任何内容。我很欣赏任何解决方法,但我更愿意了解正在发生的事情并按原样修复此映射(尽可能多)。
更新:如果我切换到干净的
继承映射策略,生成的模式看起来应该是这样并且测试通过。这是一个足够好的解决方法,但我仍然很好奇是否有办法使混合策略正常工作。
I am experimenting with Hibernate to gain experience. I created a class Person
with two subclasses: Student
and Worker
:
public abstract class Person {
private Long id;
...
}
public class Student extends Person { ... }
Another class, Employer
, has a bidirectional one-to-many relationship with Worker
.
public class Worker extends Person {
private Employer employer;
...
}
public class Employer {
private String taxId;
private Set<Worker> employees = new HashSet<Worker>();
...
}
for which the mapping is
<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>
The inheritance hierarchy is modelled with a mixed strategy, where Student
is mapped to the PERSON
table, but Worker
is stored in its own table, joined with a foreign key:
<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>
I use Apache Derby 10.5.3.0 and autogenerate the schema by setting hibernate.hbm2ddl.auto
to create-drop
.
To test all this, I created a DBUnit test with the following dataset:
<EMPLOYER TAX_ID = "1234567890"
...
/>
<PERSON PERSON_ID = "12345"
PERSON_TYPE = "WRK"
...
/>
<WORKER WORKER_ID = "12345"
EMPLOYER_TAX_ID = "1234567890"
...
/>
I have a test which loads the worker entity and verifies that it has the correct employee. This passes. Then a test for the opposite direction:
String taxId = "1234567890";
Employer employer = (Employer) session.get(Employer.class, taxId);
assertNotNull(employer);
assertThat(employer.getEmployees().size(), is(1));
Upon execution, the last assert fails because the set of employees is empty.
Digging deeper, I found that for some reason Hibernate looks for (and creates) the EMPLOYER_TAX_ID column in table PERSON instead of WORKER! It is also present in WORKER, but that one is not used in the query. The select statements for populating the set of employees is:
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=?
Why is this? And how can I make Hibernate find EMPLOYER_TAX_ID in the WORKER table?
Note that since this is an experimental project, I can change just about anything. I appreciate any workarounds, but I would prefer understanding what's going on and fixing this mapping as it is (as much as possible).
Update: if I switch to a clean <joined-subclass>
inheritance mapping strategy, the generated schema looks as it should and the test passes. This is a good enough workaround, but I am still curious whether there is a way to make the mixed strategy work properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像是已知的错误: http://opensource.atlassian.com/projects/ hibernate/browse/HHH-1015
它自古以来就广为人知,并且已被报道过无数次。尽管如此,他们还是没有修复它......
This sounds like known bug: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1015
It is known since ages and has been reported tons of times. Still, they are not fixing it...
我遇到了同样的问题,我用这种方式解决了它:
通过 join-sublclass 更改子类。
在你的情况下,它会是:
希望这有帮助。
I faced the same problem and I fixed it in this way:
Changing subclass by joined-sublclass.
In your case it would be:
Hope this helps.