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.
This commit is contained in:
Oliver Gierke
2011-06-23 19:47:59 +02:00
parent dae766d579
commit 5d3602d53f
7 changed files with 60 additions and 27 deletions

View File

@@ -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.
*

View File

@@ -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);
}
}
}

View File

@@ -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.
*

View File

@@ -811,6 +811,17 @@ public class UserRepositoryTests {
}
@Test
public void findsUsersBySpringDataNamedQuery() {
flushTestUsers();
List<User> result = repository.findBySpringDataNamedQuery("Gierke");
assertThat(result.size(), is(1));
assertThat(result, hasItem(firstUser));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -212,4 +212,7 @@ public interface UserRepository extends JpaRepository<User, Integer>,
List<SpecialUser> findSpecialUsersByLastname(String lastname);
List<User> findBySpringDataNamedQuery(String lastname);
}

View File

@@ -0,0 +1 @@
User.findBySpringDataNamedQuery=select u from User u where u.lastname=?

View File

@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
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">
<!-- Database setup -->
<import resource="infrastructure.xml" />
@@ -12,6 +14,13 @@
<property name="customImplementation">
<bean class="org.springframework.data.jpa.repository.sample.UserRepositoryImpl" />
</property>
<property name="namedQueries">
<bean class="org.springframework.data.repository.core.support.PropertiesBasedNamedQueries">
<constructor-arg>
<util:properties location="META-INF/jpa-named-queries.properties" />
</constructor-arg>
</bean>
</property>
</bean>
<bean id="roleDao" class="org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean">