From 9734010d8e036c4c763e47575b2551a8a2f59ec9 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 17 Jul 2012 11:41:26 +0200 Subject: [PATCH] DATAJPA-226 - Working around ambiguities in the JPA spec. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../data/jpa/repository/query/SimpleJpaQuery.java | 8 +++++++- .../repository/query/JpaQueryLookupStrategyUnitTests.java | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java b/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java index ef2b7b816..0ba422464 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java @@ -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); + } } } diff --git a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java index 6e3aebce6..b7f11a137 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java @@ -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)); } }