Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 850
Keating Avatar asked Sep 12 '25 08:09

Keating


2 Answers

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);
like image 143
Mikko Maunu Avatar answered Sep 16 '25 08:09

Mikko Maunu


ListAttribute extends PluralAttribute, as does any attribute based on a collection. Did you actually try to use root.fetch(ListAttribute)?

like image 21
Rasmus Franke Avatar answered Sep 16 '25 09:09

Rasmus Franke