【发布时间】:2012-10-15 20:38:56
【问题描述】:
这基本上与此相反: How to do a paged QueryDSL query with Spring JPA?
这是针对我无法使用任何 findAll() 方法的自定义查询。
编辑:
发错链接了。现在更正了。
【问题讨论】:
标签: spring-data querydsl
这基本上与此相反: How to do a paged QueryDSL query with Spring JPA?
这是针对我无法使用任何 findAll() 方法的自定义查询。
编辑:
发错链接了。现在更正了。
【问题讨论】:
标签: spring-data querydsl
您可以这样做:但请确保修剪 o.getProperty(),以便您只传递属性而不是“别名”。+property
if (pageable != null) {
query.offset(pageable.getOffset());
query.limit(pageable.getPageSize());
for (Sort.Order o : pageable.getSort()) {
PathBuilder<Object> orderByExpression = new PathBuilder<Object>(Object.class, "object");
query.orderBy(new OrderSpecifier(o.isAscending() ? com.mysema.query.types.Order.ASC
: com.mysema.query.types.Order.DESC, orderByExpression.get(o.getProperty())));
}
}
【讨论】:
我不知道它是否仍然相关,但在 spring data jpa 中有一个实现,用于在 data.domain.Sort (Spring JPA) 对象到 OrderSpecifier (QueryDSL) 之间进行转换。
GIT Source of Querydsl Support in Spring JPA
这是一个非常丑陋的实现,但由于方法是私有的,您仍然可以将其重用于自己的目的:
public JPQLQuery applySorting(Sort sort, JPQLQuery query)
但是如果你使用 Spring data JPA,在你的自定义 Repository 实现中,你只需要这样做:
public Page<MyObject> findAll(Predicate predicate, Pageable pageable) {
QMyObject myObject = QMyObject.myObject;
JPQLQuery jPQLQuery = from(myObject)
.join(myObject.user)
.where(predicate);
jPQLQuery = getQuerydsl().applyPagination(pageable, jPQLQuery);
List<MyObject> myObjectList = jPQLQuery.list(myObject);
long count = jPQLQuery.count();
Page<MyObject> myObjectPage = new PageImpl<MyObject>(myObjectList, pageable, count);
return myObjectPage;
}
希望对您有所帮助!
【讨论】:
org.springframework.data.domain.Sort.Order 和 com.querydsl.core.types.Order 非常相似,但两者之间没有直接的转换。这是frozenfury 的改进版本@ 答案:
PathBuilder<Entity> entityPath = new PathBuilder<>(Entity.class, "entity");
for (Order order : pageable.getSort()) {
PathBuilder<Object> path = entityPath.get(order.getProperty());
query.orderBy(new OrderSpecifier(com.querydsl.core.types.Order.valueOf(order.getDirection().name()), path));
}
【讨论】:
private OrderSpecifier<?>[] getSortedColumn(Sort sorts){
return sorts.toList().stream().map(x ->{
Order order = x.getDirection().name() == "ASC"? Order.ASC : Order.DESC;
SimplePath<Object> filedPath = Expressions.path(Object.class, Qobject, x.getProperty());
return new OrderSpecifier(order, filedPath);
}).toArray(OrderSpecifier[]::new);
}
那么你可以这样使用:
.where(...),
.orderBy(getSortedColumn(pageable.getSort()))
【讨论】:
objectDTO?
或者,如果您想直接将排序应用到基本查询中而无需转换为 OrderSpecifier>,您可以利用 org.springframework.data.jpa.repository.support.Querydsl。
JPQLQuery<?> query = new JPAQuery<>(entityManager);
//prepare your base query
query.select(<YourQEntity>).from(<YourQEntity>).where(<whereClause>);
Querydsl querydsl = new Querydsl(entityManager, (new PathBuilderFactory()).create(<YourEntityClass>.class));
//apply org.springframework.data.domain.Sort
querydsl.applySorting(pageable.getSort(), query);
【讨论】: