简体   繁体   English

使用子类连接表中的外键映射hibernate多对一

[英]Map hibernate many-to-one with the foreign key in subclass joined table

I'm mapping some entities using Hibernate 3 for my project and simply explained I've got kind of this: 我正在使用Hibernate 3为我的项目映射一些实体,并简单解释我有这样的:

  • Student entity ( tstudent table) Student实体( tstudent表)
  • UniversityStudent entity ( tuniversitystudent table) UniversityStudent实体( tuniversitystudent表)
  • University entity ( tuniversity table) University实体( tuniversity表)

UniversityStudent extends from Student and has its own attributes, like the university itself, which is a foreign key into the tuniversitystudent table. UniversityStudent Student延伸自Student并拥有自己的属性,如大学本身,这是一个外键进入tuniversitystudent表。 It is also mapped like a subclass into the Student class, using a discriminator field: 它还使用discriminator字段将子类映射到Student类:

<class name="mycompany.Student" table="tstudent" discriminator-value="BASIC">
    <id name="id" column="id" type="integer">
        <generator class="native" />
    </id>
    <discriminator column="type" />
    <property name="name" column="name" />
    <property name="surname" column="surname" />
    <property name="phoneNumber" column="phone_number" />
    <subclass discriminator-value="UNIVERSITY"
            name="mycompany.UniversityStudent">
        <join table="tuniversitystudent">
            <key column="id_student" />
            <many-to-one name="university" class="mycompany.University">
                <column name="id_university" />
            </many-to-one>
        </join>
    </subclass>
</class>

Well, now I want to have a Set collection with the UniversityStudent entities for each University . 那么,现在我希望每个University都有一个Set集合与UniversityStudent实体。 So I map it like that: 所以我这样映射:

<class name="mycompany.University" table="tuniversity">
    <id name="id" column="id" type="integer">
        <generator class="native" />
    </id>
    <property name="name" column="name" />
    <set name="universityStudents" table="tuniversitystudent">
        <key>
            <column name="id_university" />
        </key>
        <one-to-many class="mycompany.UniversityStudent" />
    </set>
</class>

My problem comes when I want to load a University object, Hibernate complains that id_university doesn't exist in tstudent table. 我的问题是当我想要加载一个University对象,Hibernate的抱怨id_universitytstudent表中。 I checked the generated SQL query and it really tries to load it from tstudent . 我检查了生成的SQL查询,它确实尝试从tstudent加载它。

Unknown column 'student0_.id_university' in 'field list' '字段列表'中的未知列'student0_.id_university'

It seems that it's recognizing that it is a subclass of the basic Student and tries to join the collection using a field in the parent table, but however the field is actually in the child table, because only university students can have a University assigned. 它似乎认识到它是基本Student的子类并尝试使用父表中的字段加入集合,但是该字段实际上在子表中,因为只有大学生才能分配大学。

I tried another workaround which seems to work but it's not valid for me, that's mapping the UniversityStudent as a joined-subclass instead of a subclass with a join inside: 我尝试了另一种似乎有效的解决方法,但它对我无效,即将UniversityStudent映射为joined-subclass而不是带有连接subclass

<joined-subclass name="mycompany.UniversityStudent" table="tuniversitystudent">
    <key column="id_student" />
    <many-to-one name="university" class="mycompany.University">
        <column name="id_university" />
    </many-to-one>
</joined-subclass>

However, I'm interested in keeping it as a subclass with a discriminator value. 但是,我有兴趣将它保留为具有鉴别器值的子类。 Any idea? 任何的想法?

I checked out some resources and finally got into this bug: https://hibernate.atlassian.net/browse/HHH-1015 , which looks absolutely compatible with your case. 我检查了一些资源,最后遇到了这个错误: https//hibernate.atlassian.net/browse/HHH-1015 ,看起来与你的情况完全兼容。 Checkout this old question as well , again very similar to your case. 检查这个老问题 ,再次非常类似于你的情况。
I firstly read the definition of table per sublass given by Hibernate (I know, it is for version 3.3 but I couldn't find the same source for Hibernate 4): joined-subclass seems (to me) to be a custom implementation of subclass using a discriminator provided by Hibernate and that is a good reason to stay away from its usage. 我首先阅读Hibernate给出的每个子类的定义(我知道,它适用于版本3.3,但我找不到Hibernate 4的相同源代码): joined-subclass似乎(对我来说)是subclass using a discriminator的自定义实现subclass using a discriminator Hibernate提供subclass using a discriminator ,这是远离其使用的一个很好的理由。 However, from what I know, the mappings table per sublass and table per subclass using a discriminator should be equivalent, that's why I believe the bug I pointed you out is really still open. 但是,据我所知, 使用鉴别器的每个子类每个子类的 的映射应该是等价的,这就是为什么我相信我指出的错误仍然是开放的。

If you have time and will, you can try to use another JPA provider and check if you still run in the same issue. 如果您有时间和意愿,您可以尝试使用其他JPA提供程序并检查您是否仍在同一问题中运行。 JPA 2.0 specifications is a thing, provider implementation is another! JPA 2.0规范是一个东西,提供者实现是另一个! I recently run into another bug (related to @IdClass ) which forced me to try EclipseLink and the configuration which was not working with Hibernate was right with Eclipse Link 我最近遇到了另一个错误(与@IdClass相关),这个错误迫使我尝试使用EclipseLink,并且使用Eclipse链接的配置不适用于Hibernate

Seems you can use Custom SQL (or HQL) for loading . 似乎您可以使用自定义SQL(或HQL)进行加载 Haven't tried it myself, but looks like, hmm, at least as a last resort, it provides a decent solution. 没有自己试过,但看起来,嗯,至少作为最后的手段,它提供了一个体面的解决方案。

Define the query in your HBM: 在HBM中定义查询:

<sql-query name="universityStudents">
    <load-collection alias="unistu" role="University.universityStudents"/>
    SELECT unistu.*, student.*
    FROM tuniversitystudent unistu 
    JOIN tstudent student 
    ON unistu.id_student = student.id
    WHERE unistu.id_university = :id
</sql-query>

And then use it inside University : 然后在University里面使用它:

<set name="universityStudents" inverse="true">
    <key/>
    <one-to-many class="mycompany.UniversityStudent"/>
    <loader query-ref="universityStudents"/>
</set>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM