15
15
*/
16
16
package org .springframework .data .repository .query ;
17
17
18
+ import java .lang .reflect .Method ;
19
+
20
+ import org .springframework .data .repository .core .RepositoryException ;
21
+
18
22
/**
19
- * Exception to be thrown if a query cannot be created from a {@link QueryMethod }.
23
+ * Exception to be thrown if a query cannot be created from a {@link Method }.
20
24
*
21
25
* @author Oliver Gierke
26
+ * @author Mark Paluch
22
27
*/
23
- public final class QueryCreationException extends RuntimeException {
28
+ public final class QueryCreationException extends RepositoryException {
24
29
25
30
private static final long serialVersionUID = -1238456123580L ;
26
31
private static final String MESSAGE_TEMPLATE = "Could not create query for method %s! Could not find property %s on domain class %s." ;
27
32
33
+ private final Method method ;
34
+
35
+ /**
36
+ * Creates a new {@link QueryCreationException}.
37
+ */
38
+ private QueryCreationException (String message , QueryMethod method ) {
39
+
40
+ super (message , method .getMetadata ().getRepositoryInterface ());
41
+ this .method = method .getMethod ();
42
+ }
43
+
28
44
/**
29
45
* Creates a new {@link QueryCreationException}.
30
- *
31
- * @param method
32
46
*/
33
- private QueryCreationException (String message ) {
47
+ private QueryCreationException (String message , Throwable cause , Class <?> repositoryInterface , Method method ) {
34
48
35
- super (message );
49
+ super (message , cause , repositoryInterface );
50
+ this .method = method ;
36
51
}
37
52
38
53
/**
@@ -45,7 +60,7 @@ private QueryCreationException(String message) {
45
60
public static QueryCreationException invalidProperty (QueryMethod method , String propertyName ) {
46
61
47
62
return new QueryCreationException (String .format (MESSAGE_TEMPLATE , method , propertyName , method .getDomainClass ()
48
- .getName ()));
63
+ .getName ()), method );
49
64
}
50
65
51
66
/**
@@ -57,7 +72,8 @@ public static QueryCreationException invalidProperty(QueryMethod method, String
57
72
*/
58
73
public static QueryCreationException create (QueryMethod method , String message ) {
59
74
60
- return new QueryCreationException (String .format ("Could not create query for %s! Reason: %s" , method , message ));
75
+ return new QueryCreationException (String .format ("Could not create query for %s! Reason: %s" , method , message ),
76
+ method );
61
77
}
62
78
63
79
/**
@@ -68,7 +84,29 @@ public static QueryCreationException create(QueryMethod method, String message)
68
84
* @return
69
85
*/
70
86
public static QueryCreationException create (QueryMethod method , Throwable cause ) {
87
+ return new QueryCreationException (cause .getMessage (), cause , method .getMetadata ().getRepositoryInterface (),
88
+ method .getMethod ());
89
+ }
71
90
72
- return create (method , cause .getMessage ());
91
+ /**
92
+ * Creates a new {@link QueryCreationException} for the given {@link QueryMethod} and {@link Throwable} as cause.
93
+ *
94
+ * @param method
95
+ * @param cause
96
+ * @return
97
+ * @since 2.5
98
+ */
99
+ public static QueryCreationException create (String message , Throwable cause , Class <?> repositoryInterface ,
100
+ Method method ) {
101
+ return new QueryCreationException (String .format ("Could not create query for %s! Reason: %s" , method , message ),
102
+ cause , repositoryInterface , method );
103
+ }
104
+
105
+ /**
106
+ * @return
107
+ * @since 2.5
108
+ */
109
+ public Method getMethod () {
110
+ return method ;
73
111
}
74
112
}
0 commit comments