开发者

Hibernate: What's wrong with this mapping to a subclass joined on foreign key?

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开发者_JS百科_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.


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


I faced the same problem and I fixed it in this way:
Changing subclass by joined-sublclass.
In your case it would be:

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

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜