如何将同一层次结构中每个子类的集合映射到一个拥有的类? [英] How to map collection of each subclass from same hierarchy onto one owning class?

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

问题描述

有没有人知道如何使用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


org.hibernate.WrongClassException:具有id的对象:gM4pnnoRge4Odx4lUTZNMQ不是指定的子类:my.package.SubClassTwo(加载的对象是错误的类class my.package.SubClassOne)
at org.hibernate .loader.Loader.instanceAlreadyLoaded(Loader.java:1420)[hibernate-core-4.0.0.Final.jar:4.0.0.Final]
org.hibernate.loader.Loader.getRow(Loader.java) :1373)[hibernate-core-4.0.0.Final.jar:4.0.0.Final]
org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640)[hibernate-core-4.0。 0.Final.jar:4.0.0.Final]
org.hibernate.loader.Loader.doQuery(Loader.java:856)[hibernate-core-4.0.0.Final.jar:4.0.0。最终]
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)[hibernate-core-4.0.0.Final.jar :4.0.0.Fin
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)[hibernate-core-4.0.0.Final.jar:4.0.0.Fin
at org.hibernate.loader.Loader.loadCollection(Loader.java:2175)[hibernate-core-4.0.0.Final.jar:4.0.0.Final]
at org.hibernate.loader.collection.CollectionLoader。 initialize(CollectionLoader.java:61)[hibernate-core-4.0.0.Final.jar:4.0.0.F
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622)[ hibernate-co
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEvent
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606)[hibernate-core-4.0.0 .Final.jar:4.0.0.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379)[hibernate-c
at o rg.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112)[hibernate-core-4。
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266)[hibernate-core-4.0.0.Final.jar:4.0.0.Final]

org.hibernate.WrongClassException: Object with id: gM4pnnoRge4Odx4lUTZNMQ was not of the specified subclass: my.package.SubClassTwo (loaded object was of wrong class class my.package.SubClassOne) at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1420) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] at org.hibernate.loader.Loader.getRow(Loader.java:1373) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] at org.hibernate.loader.Loader.doQuery(Loader.java:856) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) [hibernate-core-4.0.0.Final.jar:4.0.0.Fin at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) [hibernate-core-4.0.0.Final.jar:4.0.0.Fin at org.hibernate.loader.Loader.loadCollection(Loader.java:2175) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61) [hibernate-core-4.0.0.Final.jar:4.0.0.F at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:622) [hibernate-co at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEvent at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1606) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:379) [hibernate-c at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112) [hibernate-core-4. at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266) [hibernate-core-4.0.0.Final.jar:4.0.0.Final]

我想要发生的是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 <来解决这个问题/ code>属于 @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;

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

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