criteria api--root.fectch() how to fetch a collection?

微笑、不失礼 提交于 2020-01-14 13:46:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!