-
Notifications
You must be signed in to change notification settings - Fork 1.5k
NamedQuery mangles query during processing. #3085
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
Comments
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
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. |
And to be clear, #2990 was a problem due to our parser not handling |
That was also the workoaround for 2990 and that’s why i related the 2 issues. |
Okay, I've traced things back to 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);
}
} |
@gregturn Is there any update on this issue? |
@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. |
Let's give it a try. |
Merged to |
Issue related to #2990.
Test case to replicate it is available in error2990_2
The text was updated successfully, but these errors were encountered: