3

方法 fetch() 的 args 类型可以是 SingularAttribute、PluralAttribute,为什么不能是 ListAttribute?

那么,如何使用 critria api 获取集合?谢谢你。

4

2 回答 2

3

正如拉斯穆斯·弗兰克所说,当然可以。只需从javadocs 中检查 FetchParent 或试试这个:

@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);
于 2011-10-01T07:49:28.230 回答
2

ListAttribute 扩展了 PluralAttribute,任何基于集合的属性也是如此。你真的尝试过使用 root.fetch(ListAttribute) 吗?

于 2011-03-04T14:49:38.447 回答