我使用EJB 3.我有基类A,并且有依赖类B,C,D
@Entity
@Table(name = "A")
public class A implements Serializable {
@OneToMany(cascade = CascadeType.ALL)
private List<B> bs;
@OneToMany(cascade = CascadeType.ALL)
private List<C> cs;
@OneToMany(cascade = CascadeType.ALL)
private List<D> ds;
}
我有疑问。如何热切地加载所有表?我想使用em.find(A.class, id);
答案 0 :(得分:1)
您必须使用OneToMany注释中的获取属性,以便:
@Entity
@Table(name = "A")
public class A implements Serializable {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<B> bs;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<C> cs;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<D> ds;
}
答案 1 :(得分:0)
我找到了问题的解决方案。
@Entity
@Table(name = "A")
public class A implements Serializable {
@OneToMany(cascade = CascadeType.ALL)
@Fetch(FetchMode.SUBSELECT)
private List<B> bs;
@OneToMany(cascade = CascadeType.ALL)
@Fetch(FetchMode.SUBSELECT)
private List<C> cs;
@OneToMany(cascade = CascadeType.ALL)
@Fetch(FetchMode.SUBSELECT)
private List<D> ds;
}
解决方案是@Fetch(FetchMode.SUBSELECT)。有一篇关于
的好文章