DATAJPA-226 - Working around ambiguities in the JPA spec.
We now expect a much broader range of exceptions possibly popping up from EntityManager.createQuery(…). It turns out the spec is not very strict about what must be returned from the call in case the provided query String is invalid. E.g. Hibernate seems to throw an IllegalStateException in case the query seems generally acceptable but has a typo in some keyword. We now catch RuntimeException invoking the call and simply rethrow the original exception if it is an IllegalArgumentException indeed but wrap any other into an IAE. See http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
This commit is contained in:
@@ -65,7 +65,13 @@ final class SimpleJpaQuery extends AbstractJpaQuery {
|
||||
|
||||
// Try to create a Query object already to fail fast
|
||||
if (!method.isNativeQuery()) {
|
||||
em.createQuery(queryString);
|
||||
try {
|
||||
em.createQuery(queryString);
|
||||
} catch (RuntimeException e) {
|
||||
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
|
||||
// http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
|
||||
throw e instanceof IllegalArgumentException ? e : new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,13 +62,14 @@ public class JpaQueryLookupStrategyUnitTests {
|
||||
Method method = UserRepository.class.getMethod("findByFoo", String.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
|
||||
|
||||
Exception reference = new IllegalArgumentException();
|
||||
Throwable reference = new RuntimeException();
|
||||
when(em.createQuery(anyString())).thenThrow(reference);
|
||||
|
||||
try {
|
||||
strategy.resolveQuery(method, metadata, namedQueries);
|
||||
} catch (Exception e) {
|
||||
assertThat(e, is(reference));
|
||||
assertThat(e, is(instanceOf(IllegalArgumentException.class)));
|
||||
assertThat(e.getCause(), is(reference));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user