@@ -41,37 +41,39 @@ final class SimpleJpaQuery extends AbstractStringBasedJpaQuery {
41
41
*
42
42
* @param method must not be {@literal null}
43
43
* @param em must not be {@literal null}
44
- * @param countQueryString
44
+ * @param sourceQuery the original source query, must not be {@literal null} or empty.
45
45
* @param queryRewriter must not be {@literal null}
46
46
* @param evaluationContextProvider must not be {@literal null}
47
47
* @param parser must not be {@literal null}
48
48
*/
49
- public SimpleJpaQuery (JpaQueryMethod method , EntityManager em , @ Nullable String countQueryString ,
50
- QueryRewriter queryRewriter , QueryMethodEvaluationContextProvider evaluationContextProvider , SpelExpressionParser parser ) {
51
- this (method , em , method .getRequiredAnnotatedQuery (), countQueryString , queryRewriter , evaluationContextProvider , parser );
49
+ public SimpleJpaQuery (JpaQueryMethod method , EntityManager em , @ Nullable String sourceQuery ,
50
+ QueryRewriter queryRewriter , QueryMethodEvaluationContextProvider evaluationContextProvider ,
51
+ SpelExpressionParser parser ) {
52
+ this (method , em , method .getRequiredAnnotatedQuery (), sourceQuery , queryRewriter , evaluationContextProvider ,
53
+ parser );
52
54
}
53
55
54
56
/**
55
57
* Creates a new {@link SimpleJpaQuery} that encapsulates a simple query string.
56
58
*
57
59
* @param method must not be {@literal null}
58
60
* @param em must not be {@literal null}
59
- * @param queryString must not be {@literal null} or empty
61
+ * @param sourceQuery the original source query, must not be {@literal null} or empty
60
62
* @param countQueryString
61
63
* @param queryRewriter
62
64
* @param evaluationContextProvider must not be {@literal null}
63
65
* @param parser must not be {@literal null}
64
66
*/
65
- public SimpleJpaQuery (JpaQueryMethod method , EntityManager em , String queryString , @ Nullable String countQueryString , QueryRewriter queryRewriter ,
66
- QueryMethodEvaluationContextProvider evaluationContextProvider , SpelExpressionParser parser ) {
67
+ public SimpleJpaQuery (JpaQueryMethod method , EntityManager em , String sourceQuery , @ Nullable String countQueryString ,
68
+ QueryRewriter queryRewriter , QueryMethodEvaluationContextProvider evaluationContextProvider ,
69
+ SpelExpressionParser parser ) {
67
70
68
- super (method , em , queryString , countQueryString , queryRewriter , evaluationContextProvider , parser );
71
+ super (method , em , sourceQuery , countQueryString , queryRewriter , evaluationContextProvider , parser );
69
72
70
- validateQuery (getQuery (). getQueryString () , "Validation failed for query for method %s" , method );
73
+ validateQuery (getQuery (), "Validation failed for query %s for method %s" , method );
71
74
72
75
if (method .isPageQuery ()) {
73
- validateQuery (getCountQuery ().getQueryString (),
74
- String .format ("Count query validation failed for method %s" , method ));
76
+ validateQuery (getCountQuery (), "Count query %s validation failed for method %s" , method );
75
77
}
76
78
}
77
79
@@ -81,23 +83,24 @@ public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryStrin
81
83
* @param query
82
84
* @param errorMessage
83
85
*/
84
- private void validateQuery (String query , String errorMessage , Object ... arguments ) {
86
+ private void validateQuery (DeclaredQuery query , String errorMessage , JpaQueryMethod method ) {
85
87
86
88
if (getQueryMethod ().isProcedureQuery ()) {
87
89
return ;
88
90
}
89
91
90
92
EntityManager validatingEm = null ;
93
+ var queryString = query .getQueryString ();
91
94
92
95
try {
93
96
validatingEm = getEntityManager ().getEntityManagerFactory ().createEntityManager ();
94
- validatingEm .createQuery (query );
97
+ validatingEm .createQuery (queryString );
95
98
96
99
} catch (RuntimeException e ) {
97
100
98
101
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
99
- // https://java.net/projects /jpa-spec/lists/jsr338-experts/archive/ 2012- 07/message/17
100
- throw new IllegalArgumentException (String . format ( errorMessage , arguments ), e );
102
+ // https://download.oracle.com/javaee-archive /jpa-spec.java.net/users/ 2012/ 07/0404.html
103
+ throw new IllegalArgumentException (errorMessage . formatted ( query , method ), e );
101
104
102
105
} finally {
103
106
0 commit comments