Skip to content

Commit

Permalink
incorporating feedback from vladmihalcea#673 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymajoros2 committed Nov 13, 2023
1 parent c4e39ef commit 8b260be
Showing 1 changed file with 4 additions and 18 deletions.
Expand Up @@ -130,25 +130,11 @@ private static List<Object> getSQLParametersFromJPAQuery(Query query) {
* @return the unproxied Hibernate query, or original query if there is no proxy, or null if it's not an Hibernate query of required type
*/
private static Optional<QuerySqmImpl<?>> getSqmQueryOptional(Query query) {
try {
if (query instanceof QuerySqmImpl) {
QuerySqmImpl<?> querySqm = (QuerySqmImpl<?>) query;
return Optional.of(querySqm);
}
if (!Proxy.isProxyClass(query.getClass())) {
return Optional.empty();
}
// is proxyied, get it out
InvocationHandler invocationHandler = Proxy.getInvocationHandler(query);
Class<?> innerClass = invocationHandler.getClass();
Field targetField = innerClass.getDeclaredField("target");
targetField.setAccessible(true);
QuerySqmImpl<?> querySqm = (QuerySqmImpl<?>) targetField.get(invocationHandler);
Query unwrappedQuery = query.unwrap(Query.class);
if (unwrappedQuery instanceof QuerySqmImpl) {
QuerySqmImpl<?> querySqm = (QuerySqmImpl<?>) unwrappedQuery;
return Optional.of(querySqm);
} catch (NoSuchFieldException exception) {
return Optional.empty(); // not an Hibernate query
} catch (IllegalAccessException exception) {
throw new IllegalStateException(exception);
}
return Optional.empty();
}
}

0 comments on commit 8b260be

Please sign in to comment.