2

我有这两个类:

@Entity
public abstract class Compound {


    @OneToMany(fetch = FetchType.EAGER, mappedBy="compound",
        targetEntity=Containable.class, cascade = CascadeType.ALL)      
    private Set<Containable> containables = new HashSet<>();
}

@Entity 
public abstract class Containable {     

    @ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Compound compound;
}

我想以类型安全的方式实现的是 Compound 的特定实现只接受 Containable 的特定实现,反之亦然。

我怎样才能做到这一点?

编辑:

我已经得到了asenovm的解决方案,只是想仔细检查一下它实际上是正确的。

我的后续问题是,如果我有class Compound<T extends Containable>并且class Containable<T extends Compound>Containable 和 Compound 是原始类型,还是我弄错了?因为在class Compound<T extends Containable>T 实际上是一个 Containable 而不是其他任何东西。

4

1 回答 1

2

大概是这样的?

@Entity
public abstract class Compound<T extends Containable> {


    @OneToMany(fetch = FetchType.EAGER, mappedBy="compound",
        targetEntity=Containable.class, cascade = CascadeType.ALL)      
    private Set<T> containables = new HashSet<T>();

}

@Entity 
public abstract class Containable<T extends Compound> {     

    @ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private T compound;
}
于 2013-02-08T12:00:30.447 回答