如何将同一层次结构中每个子类的集合映射到一个拥有的类

如何将同一层次结构中每个子类的集合映射到一个拥有的类

本文介绍了如何将同一层次结构中每个子类的集合映射到一个拥有的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何使用JPA 2.0注释将同一层次结构中的每个子类的集合映射到一个拥有的类,由Hibernate 4.0.0.Final支持?

Does anyone know how to map a collection of each subclass from the same hierarchy onto one owning class, using JPA 2.0 annotations, backed by Hibernate 4.0.0.Final?

比特复杂,所以这是一个例子。层次结构类如下所示:

Bit convoluted, so here's an example. Hierarchy classes look like this:


    @Entity
    @Cacheable(true)
    @Table(name = "hierarcicals")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name = "class", discriminatorType = DiscriminatorType.STRING)
    public abstract class MySuperClass {
    ...




    @Entity
    @DiscriminatorValue("SubClassOne")
    public class SubClassOne extends MySuperClass {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner")
    private Owner owner;




    @Entity
    @DiscriminatorValue("SubClassTwo")
    public class SubClassTwo extends MySuperClass {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner")
    private Owner owner;

这一切看起来都很好,当我持久保存对象时,我可以看到它们进入数据库正确的识别者和正确的所有者。

This all looks fine, and when I persist objects, I can see them going into the database with the right discriminators, and the right owner.

这是我的拥有类 - 其注释没什么特别之处:

Here's my 'owning' class - nothing special about its annotations:

@Entity
@Cacheable(true)
@Table(name = "owner")
public class Owner  {
...
@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY)
private List<SubClassOne> subClassOnes;

@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY)
private List<SubClassTwo> subClassTwos;
...

2个List集合有getter。当我调用getSubClassOnes()时,我得到了一包所有关联的实体,类型为SubClassOne,甚至是那些在数据库中具有SubClassTwo鉴别器的实体。当我调用getSubClassTwos()时,我得到的东西似乎是一包SubClassTwos,但是当我在运行时迭代该集合时,会爆炸

The 2 List collections have getters. When I call getSubClassOnes(), I get a bag of ALL the associated entities back, as type SubClassOne, even those that have the SubClassTwo discriminator in the database. When I call getSubClassTwos(), I get what appears to be a bag of SubClassTwos, but which, when I iterate over the collection at runtime, blows up with

我想要发生的是getSubClassOnes()返回一个集合,其中包含与类SubClassOnes相关联的集合,而getSubClassTwos()返回一个具有相关性的集合SubClassTwos。有谁知道我怎么做到这一点?

What I'd like to happen is that getSubClassOnes() returns a collection containing those associated with the owner with class SubClassOnes, and getSubClassTwos() returns a collection with the relevant SubClassTwos. Does anyone know how I can achieve this?

推荐答案

对于我们来说,通过添加 targetEntity 属于 @OneToMany 注释,指向超类,如下所示:

For us this was fixed by adding the targetEntity attribute to the @OneToMany annotation, pointing to the super class, like so:

@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY, targetEntity = MySuperClass.class)
private List<SubClassOne> subClassOnes;

@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY, targetEntity = MySuperClass.class)
private List<SubClassTwo> subClassTwos;

这篇关于如何将同一层次结构中每个子类的集合映射到一个拥有的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:45