Hibernate Criteria API Invalid Path generatedAlias4 with correlated subquery

Description

I am having an invalid path issue with Hibernate and Spring JPA Criteria API. I have the following error:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: ‘generatedAlias4.applicationId’ [select generatedAlias0 from ie.ul.ethics.scieng.applications.models.applications.Application as generatedAlias0 where :param0 in (select generatedAlias1.username from ie.ul.ethics.scieng.applications.models.applications.SubmittedApplication as generatedAlias2 inner join generatedAlias2.assignedCommitteeMembers as generatedAlias3 inner join generatedAlias3.user as generatedAlias1 where generatedAlias3.applicationId=generatedAlias4.applicationId)

Basically, the alias generatedAlias4 is presumably an alias that was meant to appear from a JOIN performed somewhere but the alias is not defined anywhere in the SQL leading to the error.

The SQL I am trying to emulate is:

SELECT a FROM SubmittedApplication a WHERE ‘test’ IN(SELECT m.username FROM SubmittedApplication a1 JOIN AssignedCommitteeMember assigned JOIN User WHERE a.applicationId = a1.applicationId);

I understand that might not be correct HQL (quickly wrote it to give a rough idea) but I am using the CriteriaQuery and CriteriaBuilder API to dynamically create the query.

The code creating the query is as follows:

It returns a Predicate for use in a Specification to search using a repository extending the JpaSpecificationExecutor interface. I used correlate to correlate the application root into the subquery, which theoretically means the last of the subquery where statement should be where generatedAlias3.applicationId=generatedAlias0.applicationId and not generatedAlias3.applicationId=generatedAlias4.applicationId

The query is meant to ask, find all Applications where a user with the username ‘test’ exists in the list of AssignedCommitteeMembers. The SubmittedApplication class is a subclass of the Application class (a class that represents an application form being created on the system) that contains a list with OneToMany mapping of these AssignedCommitteeMembers. The first I noticed, selects from the base Application class. Shouldn’t it be selected from the SubmittedApplication class? Could this be the issue? select generatedAlias0 from ie.ul.ethics.scieng.applications.models.applications.Application as generatedAlias0 instead of select generatedAlias0 from ie.ul.ethics.scieng.applications.models.applications.SubmittedApplication as generatedAlias0

The entity for the SubmittedApplication class (with irrelevant properties stripped. The applicationId field is contained in the superclass Application. It is a String ID separate to the database ID used for application management):

The AssignedCommitteeMember class is here:

 

I was told by Christian Beikov that the issue has been fixed as of Version 6.0 but that is not compatible with Spring Boot yet. I have also tried the same query without the call to correlate

Activity

Details

Assignee

Reporter

Components

Affects versions

Priority

Created March 15, 2022 at 9:36 AM
Updated December 3, 2024 at 7:39 AM