我有以下3个实体(注意懒集合的层次结构):
@Entity
public class ObjectEntity {
@OneToMany(fetch = FetchType.LAZY)
Set<ObjectInstanceEntity> instances;
}
@Entity
public class ObjectInstanceEntity {
@OneToMany(fetch = FetchType.LAZY)
Set<ObjectInstanceClientEntity> clients;
}
@Entity
public class ObjectInstanceClientEntity {
//nothing special
}
我还有根实体的sping-data-jpa存储库:
public interface ObjectEntityRepository extends JpaRepository<ObjectEntity, UUID>, JpaSpecificationExecutor<ObjectEntity>{
}
我正在尝试使用<代码>页面
private final Specification<ObjectEntity> spec = new Specification<ObjectEntity>() {
@Override
public Predicate toPredicate(Root<ObjectEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
root.fetch("instances", JoinType.LEFT);
root.fetch("instances.clients", JoinType.LEFT); //this doesnt work
return cb.; //what do i put here? any(null) ?
}
};
构造谓词的正确方法是什么,以便引入所有惰性词?
fetch()方法返回一个fetch对象,该对象可以用作新Fetch()的“根”。
提取存储在Root对象中,您不必返回它。
在您的案例中:
private final Specification<ObjectEntity> spec = new Specification<ObjectEntity>() {
@Override
public Predicate toPredicate(Root<ObjectEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
root.fetch("instances", JoinType.LEFT).fetch("clients", JoinType.LEFT); //this should work
}
};