Can't compare test expression of type when upgrading to 6.2.0 or above from 6.1.7

Query code:

                CriteriaQuery<Video> criteria = cb.createQuery(Video.class);
                var root = criteria.from(Video.class);
                root.fetch("channel", JoinType.RIGHT);
                var subquery = criteria.subquery(User.class);
                var subroot = subquery.from(User.class);
                subquery.select(subroot.get("subscribed_ids"))
                        .where(cb.equal(subroot.get("id"), user.getId()));

                criteria.select(root)
                        .where(
                                root.get("channel").in(subquery)
                        )
                        .orderBy(cb.desc(root.get("uploaded")));

Stacktrace:

 java.lang.IllegalArgumentException: Can't compare test expression of type [Channel] with element of type [User]
 	at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.assertComparable(SqmCriteriaNodeBuilder.java:2098)
 	at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.implyListElementType(SqmInListPredicate.java:138)
 	at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.<init>(SqmInListPredicate.java:60)
 	at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.<init>(SqmInListPredicate.java:47)
 	at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.in(SqmCriteriaNodeBuilder.java:2618)
 	at org.hibernate.query.sqm.tree.expression.AbstractSqmExpression.in(AbstractSqmExpression.java:124)
 	at org.hibernate.query.sqm.tree.expression.AbstractSqmExpression.in(AbstractSqmExpression.java:31)
 	at me.kavin.piped.server.handlers.auth.FeedHandlers.feedResponse(FeedHandlers.java:119)

GH Link with line numbers: Piped-Backend/FeedHandlers.java at master · TeamPiped/Piped-Backend · GitHub

The code used to work on 6.1.7, has anything changed?

Sanity checks were added to help you find bugs in your code. Also see hibernate-orm/migration-guide.adoc at 6.2 · hibernate/hibernate-orm · GitHub

You have to use

var subquery = criteria.subquery(String.class);

and

root.get("channel").get("uploader_id").in(subquery)
1 Like

That was spot on, thank you!

I’m facing a similar issue, but we are using specifications (Spring boot, the code was working prior to upgrading but not anymore. I think, the solution above is relevant but not sure how to rewrite it to make it work. Below is how it is

  private Specification<RecognitionEntity> loggedInUserIsRecipient(Integer userId) {
        return (root, query, criteriaBuilder) -> {
            Subquery<RecognitionRecipientEntity> subquery = criteriaBuilder.createQuery().subquery(RecognitionRecipientEntity.class);
            Root<RecognitionRecipientEntity> recipients = subquery.from(RecognitionRecipientEntity.class);
            Subquery<RecognitionRecipientEntity> transactionIds = subquery.select(recipients.get(RecognitionRecipientEntity_.TRANSACTION_ID))
                                                                          .where(criteriaBuilder.equal(recipients.get(RecognitionRecipientEntity_.RECEIVER_ID),
                                                                                                       userId));

            return  root.get(RecognitionEntity_.ID).in(transactionIds.correlate(recipients));
        };
    }

and I’m getting exception


Can't compare test expression of type [BasicSqmPathSource(id : UUID)] with element of type [RecognitionRecipientEntity]
	at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.assertComparable(SqmCriteriaNodeBuilder.java:2102)
	at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.implyListElementType(SqmInListPredicate.java:138)

Assuming that TRANSACTION_ID is a UUID, you need to replace

Subquery<RecognitionRecipientEntity> subquery = criteriaBuilder.createQuery().subquery(RecognitionRecipientEntity.class);

with

Subquery<UUID> subquery = criteriaBuilder.createQuery().subquery(UUID.class);

The type must match what you expect to get from the select, in your case .select(recipients.get(RecognitionRecipientEntity_.TRANSACTION_ID))

1 Like

Yup - I was just able to get it done, and yes your solution is inline with that.
I really appreciate your response, it give me confirmation of what I did.