开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜