在 Hibernate 搜索模块中使用 QueryDSL 排序时出错

Error using QueryDSL sorting with Hibernate Search module

在我的存储库 class 中,我尝试使用 querydsl-hibernate-search 模块 4.1.4 版和 Spring Data JPA 1.5.2 版进行查询。当我调用方法时:

public Iterable<Incident> findAll(Predicate predicate, int offset, int limit, OrderSpecifier<?>[] orders) {
    return createQuery(predicate).orderBy(orders).offset(offset).limit(limit).fetch();
}

那个电话:

private SearchQuery<Incident> createQuery(Predicate predicate) {
    return new SearchQuery<>(getEntityManager().unwrap(HibernateEntityManager.class).getSession(), incident).where(predicate);
}

我收到这个错误:

java.lang.NoSuchMethodError: org.apache.lucene.search.SortField.<init>(Ljava/lang/String;IZ)V

查看代码源内部,我发现了问题,class 上的一个方法:LuceneSerializer,行:557:

public Sort toSort(List<? extends OrderSpecifier<?>> orderBys) {
    List<SortField> sorts = new ArrayList<SortField>(orderBys.size());
    for (OrderSpecifier<?> order : orderBys) {
        if (!(order.getTarget() instanceof Path<?>)) {
            throw new IllegalArgumentException("argument was not of type Path.");
        }
        Class<?> type = order.getTarget().getType();
        boolean reverse = !order.isAscending();
        Path<?> path = getPath(order.getTarget());
        if (Number.class.isAssignableFrom(type)) {
            sorts.add(new SortField(toField(path), sortFields.get(type), reverse));
        } else {
            sorts.add(new SortField(toField(path), sortLocale, reverse));
        }
    }
    Sort sort = new Sort();
    sort.setSort(sorts.toArray(new SortField[sorts.size()]));
    return sort;
}

class SortField 的构造方法参数不正确:

sorts.add(new SortField(toField(path), sortFields.get(type), reverse));

在我看来这是querydsl-hibernate-seach和lucene之间的版本冲突问题。

据我所知herequerydsl-hibernate-search 依赖于 Hibernate Search (4.2.0.Final) 的一个古老版本。我不希望它与 Hibernate Search 5.5.6 一起使用。

自 4.2 以来发生了很多事情。0.Final,所以我认为您只有两个选择:

您可以按原样使用 querydsl-hibernate-search,但它会强制您使用非常旧版本的 Hibernate Search (not even on the migration guide anymore),这会顺便强制您使用旧版本的 Hibernate ORM ,等等...