问题
The args' type of method fetch() can be SingularAttribute, PluralAttribute, why not can't be ListAttribute ?
Then, how to fetch a collection with critria api ? Thank you.
回答1:
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);
回答2:
ListAttribute extends PluralAttribute, as does any attribute based on a collection. Did you actually try to use root.fetch(ListAttribute)?
来源:https://stackoverflow.com/questions/5024158/criteria-api-root-fectch-how-to-fetch-a-collection