我有这两个课程:
@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实际上是一个可容纳的,而不是其他任何东西。 最佳答案
大概是这样吗?
@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;
}