使用相同的数据透视表使用子类休眠多对多 [英] Hibernate many-to-many with subclasses using the same pivot table

查看:81
本文介绍了使用相同的数据透视表使用子类休眠多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有此模型架构:

Person
    |__ Student
            |__ SchoolBoy
            |__ CollegeStudent

我正在使用Hibernate 3.6,并且使用一个鉴别符列对所有类使用 tperson 表.我的映射是这样完成的:

I'm using Hibernate 3.6 and I use the tperson table for all the classes, using a discriminator column. My mapping is done like that:

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

现在,我想介绍 Course 实体,实现课程与学生之间的关系.当然,这是一个多对多的关系.假设我使用一个名为tstudent_course的数据透视表,其中包含两种类型的学生: SchoolBoy CollegeStudent .该表包含对该人本身及其所学习课程的引用.

Now I want to introduce the Course entity, implementing a relation between courses and students. Of course, this is a many-to-many relation. Let's suppose I use a pivot table named tstudent_course, which contains students of both types SchoolBoy and CollegeStudent. This table contains a reference to the person itself and the course he's studying.

现在,当我加载 Course 实体时,我想在大学生和学校学生之间有所不同.我是那样做的:

Now I want to differ between college and school students when I load the Course entity. I do it like that:

<set name="CollegeStudents" table="tstudent_course"
            inverse="true">
    <key>
        <column name="id_course" not-null="true" />
    </key>
    <many-to-many entity-name="CollegeStudent">
        <column name="id_person" not-null="true" />
    </many-to-many>
</set>

<set name="SchoolStudents" table="tstudent_course"
            inverse="true">
    <key>
        <column name="id_course" not-null="true" />
    </key>
    <many-to-many entity-name="SchoolBoy">
        <column name="id_person" not-null="true" />
    </many-to-many>
</set>

但是,作为数据透视表的表是包含对每种类型学生的引用的表,它尝试加载我的收藏集中的每个学生,并且我收到下一个异常:

However, being the pivot table a table which contains references to every type of students, it tries to load every single student in my collections and I receive the next Exception:

Object with id: 2 was not of the specified subclass: 
   CollegeStudent (loaded object was of wrong class class SchoolBoy)

Hibernate似乎在不评估我所拥有的具体学生类型的情况下进行参加,并试图在我收集的大学生中注入一个SchoolBoy.

It seems Hibernate is doing the join whithout evaluating the concrete type of student I have and tries to inject an SchoolBoy in my collection of College Students.

我该怎么做才能避免这种情况?是否可以在数据透视表中建立一种歧视?还是我必须为每种子类创建一个特定的数据透视表?

What can I do to avoid that? Is it possible to stablish a kind of discrimination in the pivot table? Or do I have to create an specific pivot table for each kind of subclass?

推荐答案

在您的集合中,您可以添加过滤器:

In your set you can add a filter:

<set name="CollegeStudents" table="tstudent_course"
            inverse="true">
    <key>
        <column name="id_course" not-null="true" />
    </key>
    <many-to-many entity-name="CollegeStudent" where="person_type='COLLEGE_STUDENT'">
        <column name="id_person" not-null="true" />
    </many-to-many>
</set>

恕我直言,如果没有该过滤器(只有一组学生),映射会更好.

IMHO the mapping would be better without that filter (just a set of all students).

这篇关于使用相同的数据透视表使用子类休眠多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆