Java泛型;“反向关系”;(和JPA)
我有两门课:Java泛型;“反向关系”;(和JPA),java,generics,jpa,Java,Generics,Jpa,我有两门课: @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 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;
}
@实体
公共抽象类复合词{
@OneToMany(fetch=FetchType.EAGER,mappedBy=“component”,
targetEntity=Containable.class,cascade=CascadeType.ALL)
私有集containables=newhashset();
}
@实体
可包含{
@manytone(可选=true,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
私有化合物;
}
我想要以类型安全的方式实现的是,component的特定实现只接受Containable的特定实现,反之亦然
我怎样才能做到这一点
编辑:
我已经从asenovm得到了解决方案,只是想再次检查它是否正确
我的后续问题是,如果我有
类component
和类Containable
Containable和component是原始类型,还是我弄错了?因为在类化合物中,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;
}
@实体
公共抽象类复合词{
@OneToMany(fetch=FetchType.EAGER,mappedBy=“component”,
targetEntity=Containable.class,cascade=CascadeType.ALL)
私有集containables=newhashset();
}
@实体
可包含{
@manytone(可选=true,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
私人T型化合物;
}
这也是我的第一个想法,但它不适用于JPA(Hibernate)和每个类的表。->外键约束冲突当为化合物a插入一个containable时,它表示“表中没有id为xyz的化合物B的记录”。对于化合物的一种方法,component.getComponents()
现在似乎返回一个列表,而不是一个列表。(不兼容类型编译错误)。对我来说没有意义。。。