Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NamedQuery mangles query during processing. #3085

Closed
teopapath opened this issue Jul 24, 2023 · 8 comments
Closed

NamedQuery mangles query during processing. #3085

teopapath opened this issue Jul 24, 2023 · 8 comments
Assignees
Labels
in: repository Repositories abstraction type: bug A general bug

Comments

@teopapath
Copy link

Issue related to #2990.
Test case to replicate it is available in error2990_2

@gregturn
Copy link
Contributor

First of all, you can get things working if you simply move the original query straight to the repository method:

    @Transactional(readOnly = true)
//    @Query(name =  Account.NAMED_QUERY_FIND_ACCOUNT_CONTACTS_BY_ACCOUNT_IDS_AND_CONTACT_TYPE)
    @Query("select new com.example.demo.error2990_2.AccountContactInfoDTO(acc.id, acc.name, VALUE(contacts)) "
            + "from Account acc "
            + "join acc.contacts contacts on KEY(contacts) = :contactType "
            + "where acc.id in (:accountIds)")
    List<AccountContactInfoDTO> findAccountContactsByAccountIdsAndContactType(List<Long> accountIds, AccountContactType contactType);

When you supply it via named query, then the original query is fed to Hibernate to pre-processing, and later extracted by String queryString = extractor.extractQueryString(query), thus yielding:

select new com.example.demo.error2990_2.AccountContactInfoDTO(acc.id, acc.name, contacts.{element})
from com.example.demo.error2990_2.Account acc
join acc.contacts contacts
on contacts.{index} = :contactType
where acc.id in (:accountIds)

This is clearly wrong, and so when Spring Data JPA tries to move forward with it, it doesn't work as expected. It appears to be partial processing on Hibernate's behalf, make me wonder if there is a better place to do this whole process.

@gregturn
Copy link
Contributor

And to be clear, #2990 was a problem due to our parser not handling != in the extracted query, which was resolved.

@teopapath
Copy link
Author

That was also the workoaround for 2990 and that’s why i related the 2 issues.

@gregturn
Copy link
Contributor

Okay, I've traced things back to HibernateUtils, the utility used to get the HQL back from the Query object. Looks like there is more than one way to get the query, and sometimes one is right, and sometimes the other one is right. So I'm

This drafted solution works with the query up above and passes our tests, but seems a bit hack, so I'm going to keep digging.

	public static String getHibernateQuery(Object query) {

		try {

			// Try the new Hibernate implementation first
			if (query instanceof SqmQuery sqmQuery) {

				String hql = sqmQuery.getQueryString();

				if (!hql.equals("<criteria>")) {
					return hql;
				}

				String sqmHql = sqmQuery.getSqmStatement().toHqlString();

				return sqmHql;
			}

			// Couple of cases in which this still breaks, see HHH-15389
		} catch (RuntimeException o_O) {}

		// Try the old way, as it still works in some cases (haven't investigated in which exactly)

		if (query instanceof Query<?> hibernateQuery) {
			return hibernateQuery.getQueryString();
		} else {
			throw new IllegalArgumentException("Don't know how to extract the query string from " + query);
		}
	}

@teopapath
Copy link
Author

@gregturn Is there any update on this issue?

@gregturn
Copy link
Contributor

gregturn commented Sep 7, 2023

@teopapath Not yet. I have drafted a solution that seems to work, but I'm not convinced we are covering all the corner cases. I'd like to get @mp911de 's feedback before moving forward.

@mp911de
Copy link
Member

mp911de commented Sep 8, 2023

Let's give it a try.

@gregturn gregturn changed the title ConverterNotFoundException exception in hql spring-data-jpa-3.1.2 NamedQuery fails to properly handle HQL query in Spring Data JPA 3.1.2. Sep 12, 2023
@gregturn gregturn changed the title NamedQuery fails to properly handle HQL query in Spring Data JPA 3.1.2. NamedQuery mangles query during processing. Sep 12, 2023
@gregturn
Copy link
Contributor

Merged to main, backported to 3.1.x and 3.0.x.

@gregturn gregturn added type: bug A general bug in: repository Repositories abstraction and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 12, 2023
@gregturn gregturn added this to the 3.0.10 (2022.0.10) milestone Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

4 participants