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

时间:2010-04-12 11:45:29

标签: java hibernate inheritance mapping one-to-many

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

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存储在自己的表中,并使用外键连接:< / p>

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

请注意,由于这是一个实验项目,我可以改变任何事情。我很欣赏任何变通方法,但我更愿意了解正在发生的事情并尽可能地修复这个映射。

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

2 个答案:

答案 0 :(得分:2)

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

众所周知,它已被报道了很多次。不过,他们还没有解决这个问题......

答案 1 :(得分:0)

我遇到了同样的问题,我以这种方式解决了这个问题:
通过join-sublclass更改子类 在你的情况下,它将是:

<joined-subclass name="Worker" table="WORKER">
    <key column="WORKER_ID"/>
    <many-to-one name="employer" column="EMPLOYER_TAX_ID" cascade="save-update"/>  
    ...  
</joined-subclass>

希望这有帮助。