Question

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);

Était-ce utile?

La solution 2

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

Hibernate Fetching Strategies

Autres conseils

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;

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top