Reputation: 876
I work With EJB 3. I have Base class A and there are dependency classes 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;
}
I have question. How can I load all tables eagerly? I want to use em.find(A.class, id);
Upvotes: 2
Views: 891
Reputation: 876
I have found solution of my question .
@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;
}
solution is @Fetch(FetchMode.SUBSELECT) . There is a good article about
Upvotes: 0
Reputation: 2738
You must use the fetch attribute in the OneToMany annotation so:
@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;
}
Upvotes: 1