Blazebit / blaze-persistence

Rich Criteria API for JPA providers
https://persistence.blazebit.com
Apache License 2.0
727 stars 85 forks source link

Sub Query and Pagination throws an error #1936

Open shopera007 opened 1 month ago

shopera007 commented 1 month ago

It seems that I'm encountering an issue with Blaze Persistence related to a query that involves a join with a subquery and pagination. When pagination is applied, it throws the following error:

 An exception occurred while filtering products
java.lang.IllegalArgumentException: org.hibernate.query.SyntaxException: At 1:2216 and token 'entity_function', no viable alternative at input 'SELECT product.id'

However, when I remove pagination, the query works as expected. I've spent the last two days debugging and searching for a solution without success. Could you please assist me in resolving this?

Here is the code I'm trying to execute:

final CriteriaBuilder<Product> criteriaBuilder = this.criteriaBuilderFactory.create(this.entityManager, Product.class, "product");

final var entityViewSetting = EntityViewSetting.create(ProductCardView.class);
final var productViewCriteriaBuilder = this.entityViewManager.applySetting(entityViewSetting, criteriaBuilder);

productViewCriteriaBuilder.with(ProductViewHistoryCTE.class)
        .from(ProductViewHistory.class, "ProductViewCTE")
        .bind("productId").select("product.id")
        .bind("viewsCount").select("count(id)")
        .end();

productViewCriteriaBuilder.innerJoinOn(ProductViewHistoryCTE.class, "ProductViewCTE")
.on("ProductViewCTE.productId").eq("id")
.end();

final Sort sort = pageable.getSort();
if (sort.isSorted()) {
    EntityViewSortUtil.applySort(this.entityViewManager, ProductCardView.class, productViewCriteriaBuilder, sort);
}

return productViewCriteriaBuilder
        .page((int) pageable.getOffset(), pageable.getPageSize())
        .orderByAsc(Product_.ID)
        .withCountQuery(false)
        .withInlineCountQuery(false)
        .getResultList();
beikov commented 1 week ago

Hi and sorry it took so long for me to respond. This looks like a bug, but to be able to look into it more deeply, I'd need a reproducer. Could you please try to create one e.g. based on one of our quickstarts?