0
votes

Let's say I have an entity:

@Getter
@Setter
@Entity
@Table(name = "rg_request")
@TypeDef(name = "listTypeConverter", typeClass = ListTypeConverter.class)
public class Request {

    @Id
    @GeneratedValue
    @Column(name = "request_id", updatable = false)
    private Long id;

    @Column(name = "shop_group_ids")
    @Type(type = "listTypeConverter")
    private List<UUID> shopGroupIds;

    @Column(name = "brand_id", columnDefinition = "uuid", updatable = false)
    @Type(type = "pg-uuid")
    private UUID brandId;
}

And I want to find using queryDsl in the database only those requests that contain in the shopGroupIds field at least one match from the passed shopGroup

public Page<Request> getRequests(Integer page, List<String> criteria,
                                 List<String> sort, Integer pageSize) {
        return requestRepository.findAll(
                getPredicate(criteria),
                PageRequest.of(page, pageSize, SortUtils.parseSortList(sort, request.orderColumn, DESC))
        );
}

private Predicate getPredicate(List<String> criteria) {
        List<UUID> shopGroupIds = shopGroupService.getShopGroupsByOwner(getCurrentUserId()).stream()
                .map(ShopGroup::getId)
                .collect(toList());
        return SearchCriteriaBuilder.builder()
                .forEntity(Request.class)
                .filters(criteria)
                .build()
                .and(request.shopGroupIds.any().in(shopGroupIds));
}

In the Page <T> findAll(Predicate predicate, Pageable pageable) method of the QuerydslJpaPredicateExecutor class, when you call querydsl.applyPagination (pageable, createQuery (predicate) .select (path)), the following query is formed:

  select request
from Request request
where ?1 = ?1 and request.brandId = ?2 and exists (select 1
from Request request_105380127
  inner join request_105380127.shopGroupIds as request_shopGroupIds_0
where request_105380127 = request and request_shopGroupIds_0 in (?3))
order by request.id asc

and when query.fetch() is called I get NullPointerException

And without this snippet: .and(request.shopGroupIds.any().in(shopGroupIds)) all works

Does anyone know what the problem might be?

ListTypeConverter:

public class ListTypeConverter {

    @Override
    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };
    }

    @Override
    public Class<?> returnedClass() {
        return List.class;
    }

    @Override
    public List<?> nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
        Array array = rs.getArray(names[0]);
        return array == null ? null : Arrays.asList((Object[]) array.getArray());
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
        Connection c = st.getConnection();
        if (value == null) {
            st.setNull(index, Types.ARRAY);
        } else {
            st.setArray(index, c.createArrayOf("text", ((List<?>) value).toArray()));
        }
    }
}
Post the full stack trace and ideally the code of the project to GithubArthur Klezovich
@ArthurKlezovich I'm debugging this issue with tests, therefore, only NPE is visible in the log, an error is thrown by QuerydslJpaPredicateExecutor inside the findAll method when the query.fetch () method is callednozikov