gpt4 book ai didi

Hibernate 如何区分加入的子类

转载 作者:行者123 更新时间:2023-12-03 20:37:42 24 4
gpt4 key购买 nike

假设我有以下实体模型案例并使用 Hibernate 3.6:

Person
|__ Student
|__ SchoolBoy
|__ CollegeStudent

我在数据库中的表是 tperson 学生 .当我使用 hibernate 映射文件时,我将其如下声明到我的 Person 实体定义中:
<joined-subclass name="Student" table="tstudent">
<key column="id_person" />
</joined-subclass>

作为类似于 CollegeStudent 的 SchoolBoy,我想为两个 (tstudent) 使用同一张 table ,有一个 学生型鉴别器列,而我保留了两个类。我的问题是,一旦您进入连接子类,Hibernate 似乎不允许使用鉴别器列。

有解决方法吗?

更新

这就是我现在所拥有的:
<class name="Person">
<id name="Id" column="id" type="integer">
<generator class="increment" />
</id>
<joined-subclass name="Student" table="tstudent">
<key column="id_person" />
<joined-subclass name="SchoolBoy" table="tschoolboy">
<key column="id_person" />
</joined-subclass>
<joined-subclass name="CollegeStudent" table="tcollegestudent">
<key column="id_person" />
</joined-subclass>
</joined-subclass>
</class>

这就是我希望在映射的 join 部分拥有的内容:
<joined-subclass name="Student" table="tstudent">
<key column="id_person" />
<discriminator column="student_type" />
<subclass name="SchoolBoy" discriminator-value="SCHOOL_BOY" />
<subclass name="CollegeStudent" discriminator-value="COLLEGE_STUDENT" />
</joined-subclass>

最佳答案

hibernate 文档说:

Hibernate does not support mixing <subclass>, <joined-subclass> and <union-subclass> mappings under the same root <class> element. It is possible to mix together the table per hierarchy and table per subclass strategies under the the same <class> element, by combining the <subclass> and <join> elements



在您的情况下,您需要将鉴别器列移动到 tperson table 并使用 join 元素为您的学生类添加额外的属性。

<class name="Person" table="tperson" discriminator-value="PERSON">
<id name="Id" column="id" type="integer">
<generator class="increment" />
</id>
<discriminator column="person_type" />
<subclass name="Student" discriminator-value="STUDENT">
<key column="id_person" />
<subclass name="SchoolBoy" discriminator-value="SCHOOL_BOY">
<join table="tstudent">
<key column="id_person" />
...
</join>
</subclass>
<subclass name="CollegeStudent" discriminator-value="COLLEGE_STUDENT">
<join table="tstudent">
<key column="id_person" />
...
</join>
</subclass>
</subclass>
</class>

您也可以在 Student 子类元素中为所有学生共有的属性添加一个连接

关于Hibernate 如何区分加入的子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17466379/

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