Spring Data JPA-PagingAndSortingRepository和Querydsl谓词不适用于findByAttribute

时间:2018-07-26 12:23:46

标签: spring-data-jpa querydsl predicate

我有以下存储库:

public interface TableRepository extends 
    PagingAndSortingRepository<Table, Integer>, 
    QuerydslPredicateExecutor<Table>, 
    QuerydslBinderCustomizer<QTable> 
{
    Page<Table> findAllByDatabaseId(Integer databaseId, Predicate predicate, Pageable pageable);

}

我正在尝试获取具有与给定谓词匹配的特定databaseId的所有表。但是我遇到了IndexOutOfBoundsException:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at java.util.Collections$UnmodifiableList.get(Collections.java:1309)
    at org.springframework.data.jpa.repository.query.QueryParameterSetterFactory$CriteriaQueryParameterSetterFactory.create(QueryParameterSetterFactory.java:271)
    at org.springframework.data.jpa.repository.query.ParameterBinderFactory.lambda$createQueryParameterSetter$1(ParameterBinderFactory.java:139)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)

这是我调用此方法的REST端点:

public Page<TableProjection> getTablesByDatabase(
    @PathVariable("databaseId") final Integer databaseId,
    final Pageable pageable,
    @QuerydslPredicate(root = Table.class) final Predicate predicate) 
{
    tableRepository.findAllByDatabaseId(databaseId, predicate, pageable);
}

也许有人让Querydsl使用findBy方法吗?

如果我只是这样打电话给findAll的话,它就可以正常工作

Page<Table> findAll(Predicate predicate, Pageable pageable);

但是它不适用于findBy:

Page<Table> findAllByDatabaseId(Integer databaseId, Predicate predicate, Pageable pageable);

2 个答案:

答案 0 :(得分:2)

不支持此功能。您可能应该只将DatabaseId上的约束添加到谓词中。

一种实现方法是对方法进行默认实现,从作为参数传递的方法中创建一个新的Predicate,并将其用于对findAll的调用中。

实际上有一个关于此问题(或类似问题)的JIRA问题。

答案 1 :(得分:0)

多亏了Jens Schauder,我才知道了。我现在正在做的是基于现有的Predicate创建一个新的databaseId并为findAll()添加约束,然后仅调用标准的public Page<TableProjection> getTablesByDatabase( @PathVariable("databaseId") final Integer databaseId, final Pageable pageable, @QuerydslPredicate(root = Table.class) final Predicate predicate) { QTable t = QTable.table; BooleanBuilder where = new BooleanBuilder(); where.and(predicate).and(t.database.id.eq(databaseId)); final Page<Table> tables = tableRepository.findAll(where, pageable); } 方法:

lst = [1923, 1924]

m1 = df.index.get_level_values('year').isin(lst)
m2 = df.index.get_level_values('item') == 'foo'

df.loc[m1 & m2, 'count'] = 3
print (df)
           count
year item       
1922 foo       0
     bar       0
1923 foo       3
     bar       0
1924 foo       3
     bar       0