criteria api--root.fectch() how to fetch a collection?
The args' type of method fetch() can be SingularAttribut开发者_StackOverflow社区e, PluralAttribute, why not can't be ListAttribute ?
Then, how to fetch a collection with critria api ? Thank you.
Of course it can, as Rasmus Franke said. Just check from the javadocs for FetchParent or try this:
@Entity
public class SomeEntity {
@Id int id;
@OneToMany List<OtherEntity> others;
}
@Entity
public class OtherEntity {
@Id int id;
}
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<SomeEntity> cq = cb.createQuery(SomeEntity.class);
Root<SomeEntity> root = cq.from(SomeEntity.class);
ListAttribute<? super SomeEntity, OtherEntity> listAttribute = root.getModel().getList("others", OtherEntity.class);
root.fetch(listAttribute, JoinType.LEFT);
cq.select(root);
ListAttribute extends PluralAttribute, as does any attribute based on a collection. Did you actually try to use root.fetch(ListAttribute)?
精彩评论