同一实体中的OneToMany和OneToOne引用相同的其他实体会导致堆栈溢出

时间:2019-03-04 13:13:23

标签: hibernate jpa

我有以下情况:

stack overflow error

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Base implements Serializable {

    @Id
    private int Id;

    private String fieldA;
    private String fieldB;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @Id
    private ContainerOfAll owner;

    //getters/setters...
}

@Entity
public class ConcreteA extends Base {

    @Basic(optional=true)
    private String someSpecialProp;

    //getters/setters...
}

@Entity
public class ConcreteB extends Base {

    @Basic(optional=true)
    private String thing;

    //getters/setters...
}

@Entity
public class ContainerOfAll {

    @Id
    private String containerId;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="owner")
    private Set<Base> elements;

    @OneToOne
    private ConcreteA specificElement;

    //getters/setters...
}

EntityManager#find()上调用ContainerOfAll时,它将堆栈溢出。通过删除与新表的@OneToOne关系来解决这个问题,新表将单个ContainerOfAll建模为单个ConcreteA。我所做的只是将private ConcreteA specificElement转换为private int concreteAId并在EJB方法中进行手动查找。

我的问题是,考虑到以下设置,如何在不添加表或不玩原始ID的情况下做到这一点?

0 个答案:

没有答案