开发者

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

开发者 https://www.devze.com 2023-02-10 18:59 出处:网络
The args\' type of method fetch() can be SingularAttribut开发者_StackOverflow社区e, PluralAttribute, why not can\'t be ListAttribute ?

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

精彩评论

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