From 5d3602d53fcf0d1d124fc53445c084e6815ec07f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 23 Jun 2011 19:47:59 +0200 Subject: [PATCH] DATACMNS-49 - Added integration for Spring Data named queries. We now support extracting query definitions into a properties file which can be configured on the JpaRepositoryFactoryBean. The namespace will look for classpath*:META-INF/jpa-named-queries.properties by default. --- .../SimpleJpaRepositoryConfiguration.java | 13 +++++++ .../query/JpaQueryLookupStrategy.java | 34 ++++++++++++------- .../jpa/repository/query/JpaQueryMethod.java | 14 -------- .../jpa/repository/UserRepositoryTests.java | 11 ++++++ .../jpa/repository/sample/UserRepository.java | 3 ++ .../META-INF/jpa-named-queries.properties | 1 + src/test/resources/application-context.xml | 11 +++++- 7 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 src/test/resources/META-INF/jpa-named-queries.properties diff --git a/src/main/java/org/springframework/data/jpa/repository/config/SimpleJpaRepositoryConfiguration.java b/src/main/java/org/springframework/data/jpa/repository/config/SimpleJpaRepositoryConfiguration.java index f4a73c2ad..09931ca7d 100644 --- a/src/main/java/org/springframework/data/jpa/repository/config/SimpleJpaRepositoryConfiguration.java +++ b/src/main/java/org/springframework/data/jpa/repository/config/SimpleJpaRepositoryConfiguration.java @@ -75,6 +75,19 @@ public class SimpleJpaRepositoryConfiguration } + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getNamedQueriesLocation() + */ + public String getNamedQueriesLocation() { + + return "classpath*:META-INF/jpa-named-queries.properties"; + } + + /** * Returns the name of the entity manager factory bean. * diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java index 324d8a42a..333194f0d 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java @@ -1,7 +1,9 @@ /* * Copyright 2008-2011 the original author or authors. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the "Li +import org.springframework.data.repository.core.NamedQueries; +cense"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * @@ -20,6 +22,7 @@ import java.lang.reflect.Method; import javax.persistence.EntityManager; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.core.NamedQueries; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy.Key; @@ -64,20 +67,21 @@ public final class JpaQueryLookupStrategy { /* * (non-Javadoc) * - * @see - * org.springframework.data.jpa.repository.query.QueryLookupStrategy - * #resolveQuery(org.springframework.data.repository.query.QueryMethod) + * @see org.springframework.data.repository.query.QueryLookupStrategy# + * resolveQuery(java.lang.reflect.Method, + * org.springframework.data.repository.core.RepositoryMetadata, + * org.springframework.data.repository.core.NamedQueries) */ public final RepositoryQuery resolveQuery(Method method, - RepositoryMetadata metadata) { + RepositoryMetadata metadata, NamedQueries namedQueries) { return resolveQuery(new JpaQueryMethod(method, metadata, provider), - em); + em, namedQueries); } protected abstract RepositoryQuery resolveQuery(JpaQueryMethod method, - EntityManager em); + EntityManager em, NamedQueries namedQueries); } /** @@ -97,7 +101,7 @@ public final class JpaQueryLookupStrategy { @Override protected RepositoryQuery resolveQuery(JpaQueryMethod method, - EntityManager em) { + EntityManager em, NamedQueries namedQueries) { return new PartTreeJpaQuery(method, em); } @@ -122,7 +126,7 @@ public final class JpaQueryLookupStrategy { @Override protected RepositoryQuery resolveQuery(JpaQueryMethod method, - EntityManager em) { + EntityManager em, NamedQueries namedQueries) { RepositoryQuery query = SimpleJpaQuery.fromQueryAnnotation(method, em); @@ -131,6 +135,12 @@ public final class JpaQueryLookupStrategy { return query; } + String name = method.getNamedQueryName(); + if (namedQueries.hasQuery(name)) { + return new SimpleJpaQuery(method, em, + namedQueries.getQuery(name)); + } + query = NamedQuery.lookupFrom(method, em); if (null != query) { @@ -170,12 +180,12 @@ public final class JpaQueryLookupStrategy { @Override protected RepositoryQuery resolveQuery(JpaQueryMethod method, - EntityManager em) { + EntityManager em, NamedQueries namedQueries) { try { - return strategy.resolveQuery(method, em); + return strategy.resolveQuery(method, em, namedQueries); } catch (IllegalStateException e) { - return createStrategy.resolveQuery(method, em); + return createStrategy.resolveQuery(method, em, namedQueries); } } } diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java index c5c6b1ce6..9fa3d5f52 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java @@ -112,20 +112,6 @@ public class JpaQueryMethod extends QueryMethod { } - /** - * Returns the name of the {@link javax.persistence.NamedQuery} this method - * belongs to. - * - * @return - */ - String getNamedQueryName() { - - Class domainClass = getDomainClass(); - return String.format("%s.%s", domainClass.getSimpleName(), - method.getName()); - } - - /** * Returns the actual return type of the method. * diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index a6df73ed0..71a2ff66c 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -811,6 +811,17 @@ public class UserRepositoryTests { } + @Test + public void findsUsersBySpringDataNamedQuery() { + + flushTestUsers(); + + List result = repository.findBySpringDataNamedQuery("Gierke"); + assertThat(result.size(), is(1)); + assertThat(result, hasItem(firstUser)); + } + + private Page executeSpecWithSort(Sort sort) { flushTestUsers(); diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java index e1ae11b78..15533b8a4 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java @@ -212,4 +212,7 @@ public interface UserRepository extends JpaRepository, List findSpecialUsersByLastname(String lastname); + + + List findBySpringDataNamedQuery(String lastname); } diff --git a/src/test/resources/META-INF/jpa-named-queries.properties b/src/test/resources/META-INF/jpa-named-queries.properties new file mode 100644 index 000000000..cc2fc7240 --- /dev/null +++ b/src/test/resources/META-INF/jpa-named-queries.properties @@ -0,0 +1 @@ +User.findBySpringDataNamedQuery=select u from User u where u.lastname=? \ No newline at end of file diff --git a/src/test/resources/application-context.xml b/src/test/resources/application-context.xml index 924511868..5efdb5b89 100644 --- a/src/test/resources/application-context.xml +++ b/src/test/resources/application-context.xml @@ -1,7 +1,9 @@ + xmlns:util="http://www.springframework.org/schema/util" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> @@ -12,6 +14,13 @@ + + + + + + +