具有OneToOne关系的

时间:2016-08-18 14:32:17

标签: hibernate java-ee

以下Hibernate实体结构由类B和C组成,它们从抽象类A继承.D应将OneToOne关系保存到B和C.

不幸的是,这会在部署EAR时导致org.hibernate.loader.MultipleBagFetchException。我使用JBoss EAP 6.1和Hibernate 4.2.7.SP1。

A类:

package my.sandbox;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class A {

    @Id
    private Long id;

    @Column
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

B组:

package my.sandbox;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "TABLE_B")
public class B extends A {

    @OneToOne(mappedBy = "b")
    private D d;

    public D getD() {
        return d;
    }

    public void setD(D d) {
        this.d = d;
    }
}

C类:

package my.sandbox;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "TABLE_C")
public class C extends A {

    @OneToOne(mappedBy = "c")
    private D d;

    public D getD() {
        return d;
    }

    public void setD(D d) {
        this.d = d;
    }

}

D类:

package my.sandbox;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "TABLE_D")
public class D {

    @OneToOne
    private B b;

    @OneToOne
    private C c;

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }

    public C getC() {
        return c;
    }

    public void setC(C c) {
        this.c = c;
    }
}

问题似乎是来自A的B和C的继承。当B和C不从A继承时,一切都很好。但由于B和C有许多相似的属性和一个共同的休眠序列,我想使用这个超类。

我试图在D中添加一个FetchType到@OneToOne(EAGER和LAZY),并删除B和C中的mappedBy。它没有帮助。

由于A的继承,我无法在底层数据库中设置从D到B和C的任何外键约束。

我的问题的任何提示?提前谢谢。

亲切的问候 斯蒂芬

0 个答案:

没有答案