DATAJPA-389 - Introduce NativeSqlQuery type for native sql queries.
Extracted the functionality specific to the handling of native queries out of SimpleJpaQuery and moved that to the new NativeJpaQuery type. Moved common functionality to AbstractStringBasedJpaQuery and used that as a new base class for SimpleJpaQuery and NativeJpaQuery. Introduced JpqQueryFactory to centralize construction of JpaQuery objects. Renamed getQuery() method in StringQuery to getQueryString(). Original pull request: #36.
This commit is contained in:
committed by
Oliver Gierke
parent
0a11d30aac
commit
3735464ec2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2011 the original author or authors.
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
* Abstract base class to implement {@link RepositoryQuery}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public abstract class AbstractJpaQuery implements RepositoryQuery {
|
||||
|
||||
@@ -164,4 +165,4 @@ public abstract class AbstractJpaQuery implements RepositoryQuery {
|
||||
* @return
|
||||
*/
|
||||
protected abstract TypedQuery<Long> doCreateCountQuery(Object[] values);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2011 the original author or authors.
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,23 +16,98 @@
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.springframework.data.repository.query.ParameterAccessor;
|
||||
import org.springframework.data.repository.query.ParametersParameterAccessor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for {@link String} based JPA queries.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
|
||||
abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
|
||||
|
||||
private final StringQuery query;
|
||||
private final StringQuery countQuery;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AbstractStringBasedJpaQuery}.
|
||||
* Creates a new {@link AbstractStringBasedJpaQuery} from the given {@link JpaQueryMethod}, {@link EntityManager} and
|
||||
* query {@link String}.
|
||||
*
|
||||
* @param method
|
||||
* @param em
|
||||
* @param method must not be {@literal null}.
|
||||
* @param em must not be {@literal null}.
|
||||
* @param queryString must not be {@literal null}.
|
||||
*/
|
||||
public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em) {
|
||||
public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, String queryString) {
|
||||
|
||||
super(method, em);
|
||||
|
||||
Assert.hasText(queryString, "Query string must not be null or empty!");
|
||||
|
||||
this.query = new ExpressionBasedStringQuery(queryString, method.getEntityInformation());
|
||||
this.countQuery = new StringQuery(method.getCountQuery() != null ? method.getCountQuery()
|
||||
: QueryUtils.createCountQueryFor(this.query.getQueryString()));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractJpaQuery#doCreateQuery(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
public Query doCreateQuery(Object[] values) {
|
||||
|
||||
ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), values);
|
||||
String sortedQueryString = QueryUtils.applySorting(query.getQueryString(), accessor.getSort(), query.getAlias());
|
||||
|
||||
Query query = createJpaQuery(sortedQueryString);
|
||||
|
||||
return createBinder(values).bindAndPrepare(query);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractJpaQuery#createBinder(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
protected ParameterBinder createBinder(Object[] values) {
|
||||
return new StringQueryParameterBinder(getQueryMethod().getParameters(), values, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an appropriate JPA query from an {@link EntityManager} according to the current {@link AbstractJpaQuery}
|
||||
* type.
|
||||
*
|
||||
* @param queryString
|
||||
* @return
|
||||
*/
|
||||
public Query createJpaQuery(String queryString) {
|
||||
return getEntityManager().createQuery(queryString);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractJpaQuery#doCreateCountQuery(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
protected TypedQuery<Long> doCreateCountQuery(Object[] values) {
|
||||
return createBinder(values).bind(getEntityManager().createQuery(countQuery.getQueryString(), Long.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the query
|
||||
*/
|
||||
public StringQuery getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the countQuery
|
||||
*/
|
||||
public StringQuery getCountQuery() {
|
||||
return countQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,13 +56,13 @@ class ExpressionBasedStringQuery extends StringQuery {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.StringQuery#getQuery()
|
||||
* @see org.springframework.data.jpa.repository.query.StringQuery#getQueryString()
|
||||
*/
|
||||
@Override
|
||||
public String getQuery() {
|
||||
public String getQueryString() {
|
||||
|
||||
if (parsedQuery == null) {
|
||||
String rawQuery = super.getQuery();
|
||||
String rawQuery = super.getQueryString();
|
||||
this.parsedQuery = renderQueryIfExpressionOrReturnQuery(rawQuery);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
|
||||
/**
|
||||
* Factory to create the appropriate {@link RepositoryQuery} for a {@link JpaQueryMethod}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
enum JpaQueryFactory {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JpaQueryFactory.class);
|
||||
|
||||
/**
|
||||
* Creates a {@link RepositoryQuery} from the given {@link QueryMethod} that is potentially annotated with
|
||||
* {@link Query}.
|
||||
*
|
||||
* @param queryMethod must not be {@literal null}.
|
||||
* @param em must not be {@literal null}.
|
||||
* @return the {@link RepositoryQuery} derived from the annotation or {@code null} if no annotation found.
|
||||
*/
|
||||
AbstractJpaQuery fromQueryAnnotation(JpaQueryMethod queryMethod, EntityManager em) {
|
||||
|
||||
LOG.debug("Looking up query for method {}", queryMethod.getName());
|
||||
return fromMethodWithQueryString(queryMethod, em, queryMethod.getAnnotatedQuery());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link RepositoryQuery} from the given {@link String} query.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param em must not be {@literal null}.
|
||||
* @param queryString must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
AbstractJpaQuery fromMethodWithQueryString(JpaQueryMethod method, EntityManager em, String queryString) {
|
||||
|
||||
if (queryString == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return method.isNativeQuery() ? new NativeJpaQuery(method, em, queryString) : //
|
||||
new SimpleJpaQuery(method, em, queryString);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2012 the original author or authors.
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,6 +29,7 @@ import org.springframework.data.repository.query.RepositoryQuery;
|
||||
* Query lookup strategy to execute finders.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public final class JpaQueryLookupStrategy {
|
||||
|
||||
@@ -111,7 +112,7 @@ public final class JpaQueryLookupStrategy {
|
||||
@Override
|
||||
protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em, NamedQueries namedQueries) {
|
||||
|
||||
RepositoryQuery query = SimpleJpaQuery.fromQueryAnnotation(method, em);
|
||||
RepositoryQuery query = JpaQueryFactory.INSTANCE.fromQueryAnnotation(method, em);
|
||||
|
||||
if (null != query) {
|
||||
return query;
|
||||
@@ -119,7 +120,7 @@ public final class JpaQueryLookupStrategy {
|
||||
|
||||
String name = method.getNamedQueryName();
|
||||
if (namedQueries.hasQuery(name)) {
|
||||
return new SimpleJpaQuery(method, em, namedQueries.getQuery(name));
|
||||
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name));
|
||||
}
|
||||
|
||||
query = NamedQuery.lookupFrom(method, em);
|
||||
@@ -131,7 +132,6 @@ public final class JpaQueryLookupStrategy {
|
||||
throw new IllegalStateException(String.format(
|
||||
"Did neither find a NamedQuery nor an annotated query for method %s!", method));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,14 +178,14 @@ public final class JpaQueryLookupStrategy {
|
||||
}
|
||||
|
||||
switch (key) {
|
||||
case CREATE:
|
||||
return new CreateQueryLookupStrategy(em, extractor);
|
||||
case USE_DECLARED_QUERY:
|
||||
return new DeclaredQueryLookupStrategy(em, extractor);
|
||||
case CREATE_IF_NOT_FOUND:
|
||||
return new CreateIfNotFoundQueryLookupStrategy(em, extractor);
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
|
||||
case CREATE:
|
||||
return new CreateQueryLookupStrategy(em, extractor);
|
||||
case USE_DECLARED_QUERY:
|
||||
return new DeclaredQueryLookupStrategy(em, extractor);
|
||||
case CREATE_IF_NOT_FOUND:
|
||||
return new CreateIfNotFoundQueryLookupStrategy(em, extractor);
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
|
||||
/**
|
||||
* {@link RepositoryQuery} implementation that inspects a {@link org.springframework.data.repository.query.QueryMethod}
|
||||
* for the existence of an {@link org.springframework.data.jpa.repository.Query} annotation and creates a JPA native
|
||||
* {@link Query} from it.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
final class NativeJpaQuery extends AbstractStringBasedJpaQuery {
|
||||
|
||||
/**
|
||||
* Creates a new {@link NativeJpaQuery} encapsulating the query annotated on the given {@link JpaQueryMethod}.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param em must not be {@literal null}.
|
||||
* @param queryString must not be {@literal null} or empty.
|
||||
*/
|
||||
public NativeJpaQuery(JpaQueryMethod method, EntityManager em, String queryString) {
|
||||
|
||||
super(method, em, queryString);
|
||||
|
||||
Parameters<?, ?> parameters = method.getParameters();
|
||||
boolean hasPagingOrSortingParameter = parameters.hasPageableParameter() || parameters.hasSortParameter();
|
||||
|
||||
if (hasPagingOrSortingParameter) {
|
||||
throw new IllegalStateException("Cannot use native queries with dynamic sorting and/or pagination!");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery#createJpaQuery(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Query createJpaQuery(String queryString) {
|
||||
return getQueryMethod().isQueryForEntity() ? getEntityManager().createNativeQuery(queryString,
|
||||
getQueryMethod().getReturnedObjectType()) : getEntityManager().createNativeQuery(queryString);
|
||||
}
|
||||
}
|
||||
@@ -17,60 +17,44 @@ package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.repository.query.ParameterAccessor;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.ParametersParameterAccessor;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
|
||||
/**
|
||||
* {@link RepositoryQuery} implementation that inspects a {@link org.springframework.data.repository.query.QueryMethod}
|
||||
* for the existanve of an {@link org.springframework.data.jpa.repository.Query} annotation and creates a JPA
|
||||
* for the existence of an {@link org.springframework.data.jpa.repository.Query} annotation and creates a JPA
|
||||
* {@link Query} from it.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
final class SimpleJpaQuery extends AbstractJpaQuery {
|
||||
final class SimpleJpaQuery extends AbstractStringBasedJpaQuery {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SimpleJpaQuery.class);
|
||||
|
||||
private final StringQuery query;
|
||||
private final StringQuery countQuery;
|
||||
|
||||
private final JpaQueryMethod method;
|
||||
/**
|
||||
* Creates a new {@link SimpleJpaQuery} encapsulating the query annotated on the given {@link JpaQueryMethod}.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param em must not be {@literal null}.
|
||||
*/
|
||||
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em) {
|
||||
this(method, em, method.getAnnotatedQuery());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link SimpleJpaQuery} that encapsulates a simple query string.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param em must not be {@literal null}.
|
||||
* @param queryString must not be {@literal null} or empty.
|
||||
*/
|
||||
SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryString) {
|
||||
public SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryString) {
|
||||
|
||||
super(method, em);
|
||||
super(method, em, queryString);
|
||||
|
||||
this.method = method;
|
||||
this.query = new ExpressionBasedStringQuery(queryString, method.getEntityInformation());
|
||||
validateQuery(getQuery().getQueryString(), String.format("Validation failed for query for method %s!", method));
|
||||
|
||||
Parameters<?, ?> parameters = method.getParameters();
|
||||
boolean hasPagingOrSortingParameter = parameters.hasPageableParameter() || parameters.hasSortParameter();
|
||||
|
||||
if (method.isNativeQuery() && hasPagingOrSortingParameter) {
|
||||
throw new IllegalStateException("Cannot use native queries with dynamic sorting and/or pagination!");
|
||||
}
|
||||
|
||||
String preparedQueryString = this.query.getQuery();
|
||||
|
||||
if (!method.isNativeQuery()) {
|
||||
validateQuery(preparedQueryString, em, String.format("Validation failed for query for method %s!", method));
|
||||
}
|
||||
|
||||
this.countQuery = new StringQuery(method.getCountQuery() != null ? method.getCountQuery()
|
||||
: QueryUtils.createCountQueryFor(preparedQueryString));
|
||||
|
||||
if (!method.isNativeQuery() && method.isPageQuery()) {
|
||||
validateQuery(this.countQuery.getQuery(), em,
|
||||
if (method.isPageQuery()) {
|
||||
validateQuery(getCountQuery().getQueryString(),
|
||||
String.format("Count query validation failed for method %s!", method));
|
||||
}
|
||||
}
|
||||
@@ -81,13 +65,12 @@ final class SimpleJpaQuery extends AbstractJpaQuery {
|
||||
* @param query
|
||||
* @param em
|
||||
*/
|
||||
private final void validateQuery(String query, EntityManager em, String errorMessage) {
|
||||
private final void validateQuery(String query, String errorMessage) {
|
||||
|
||||
EntityManager validatingEm = null;
|
||||
|
||||
try {
|
||||
|
||||
validatingEm = em.getEntityManagerFactory().createEntityManager();
|
||||
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
|
||||
validatingEm.createQuery(query);
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
@@ -103,69 +86,4 @@ final class SimpleJpaQuery extends AbstractJpaQuery {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link SimpleJpaQuery} encapsulating the query annotated on the given {@link JpaQueryMethod}.
|
||||
*/
|
||||
SimpleJpaQuery(JpaQueryMethod method, EntityManager em) {
|
||||
this(method, em, method.getAnnotatedQuery());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractJpaQuery#createBinder(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
protected ParameterBinder createBinder(Object[] values) {
|
||||
return new StringQueryParameterBinder(getQueryMethod().getParameters(), values, query);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractJpaQuery#createQuery(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
public Query doCreateQuery(Object[] values) {
|
||||
|
||||
ParameterAccessor accessor = new ParametersParameterAccessor(method.getParameters(), values);
|
||||
String sortedQueryString = QueryUtils.applySorting(query.getQuery(), accessor.getSort(), query.getAlias());
|
||||
EntityManager em = getEntityManager();
|
||||
|
||||
Query query = null;
|
||||
|
||||
if (method.isNativeQuery()) {
|
||||
query = method.isQueryForEntity() ? em.createNativeQuery(sortedQueryString, method.getReturnedObjectType()) : em
|
||||
.createNativeQuery(sortedQueryString);
|
||||
} else {
|
||||
query = em.createQuery(sortedQueryString);
|
||||
}
|
||||
|
||||
return createBinder(values).bindAndPrepare(query);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.query.AbstractJpaQuery#doCreateCountQuery(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
protected TypedQuery<Long> doCreateCountQuery(Object[] values) {
|
||||
return createBinder(values).bind(getEntityManager().createQuery(countQuery.getQuery(), Long.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link RepositoryQuery} from the given {@link org.springframework.data.repository.query.QueryMethod} that
|
||||
* is potentially annotated with {@link org.springframework.data.jpa.repository.Query}.
|
||||
*
|
||||
* @param queryMethod
|
||||
* @param em
|
||||
* @return the {@link RepositoryQuery} derived from the annotation or {@code null} if no annotation found.
|
||||
*/
|
||||
public static RepositoryQuery fromQueryAnnotation(JpaQueryMethod queryMethod, EntityManager em) {
|
||||
|
||||
LOG.debug("Looking up query for method {}", queryMethod.getName());
|
||||
|
||||
String query = queryMethod.getAnnotatedQuery();
|
||||
|
||||
return query == null ? null : new SimpleJpaQuery(queryMethod, em, query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.util.StringUtils;
|
||||
* Encapsulation of a String JPA query.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
class StringQuery {
|
||||
|
||||
@@ -88,11 +89,11 @@ class StringQuery {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JPQL query.
|
||||
* Returns the query string.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getQuery() {
|
||||
public String getQueryString() {
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,6 @@ public class ExpressionBasedStringQueryUnitTests {
|
||||
|
||||
String source = "select from #{#entityName} u where u.firstname like :firstname";
|
||||
StringQuery query = new ExpressionBasedStringQuery(source, metadata);
|
||||
assertThat(query.getQuery(), is("select from User u where u.firstname like :firstname"));
|
||||
assertThat(query.getQueryString(), is("select from User u where u.firstname like :firstname"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,13 @@ import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.data.jpa.repository.support.DefaultJpaEntityMetadata;
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityMetadata;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
|
||||
/**
|
||||
* Unit test for {@link SimpleJpaQuery}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SimpleJpaQueryUnitTests {
|
||||
@@ -122,7 +124,9 @@ public class SimpleJpaQueryUnitTests {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class);
|
||||
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
|
||||
SimpleJpaQuery jpaQuery = new SimpleJpaQuery(queryMethod, em);
|
||||
AbstractJpaQuery jpaQuery = JpaQueryFactory.INSTANCE.fromQueryAnnotation(queryMethod, em);
|
||||
|
||||
assertThat(jpaQuery instanceof NativeJpaQuery, is(true));
|
||||
|
||||
Class<?> type = Mockito.any();
|
||||
when(em.createNativeQuery(Mockito.anyString(), type)).thenReturn(query);
|
||||
@@ -137,14 +141,14 @@ public class SimpleJpaQueryUnitTests {
|
||||
public void rejectsNativeQueryWithDynamicSort() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class, Sort.class);
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsNativeQueryWithPageable() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class, Pageable.class);
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +162,7 @@ public class SimpleJpaQueryUnitTests {
|
||||
Method method = SampleRepository.class.getMethod("findByAnnotatedQuery");
|
||||
when(em.createQuery(contains("count"))).thenThrow(IllegalArgumentException.class);
|
||||
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,13 +179,27 @@ public class SimpleJpaQueryUnitTests {
|
||||
exception.expectMessage("Count");
|
||||
exception.expectMessage(method.getName());
|
||||
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
private void createSimpleJpaQuery(Method method) {
|
||||
@Test
|
||||
public void createsASimpleJpaQueryFromAnnotation() throws Exception {
|
||||
|
||||
RepositoryQuery query = createJpaQuery(SampleRepository.class.getMethod("findByAnnotatedQuery"));
|
||||
assertThat(query instanceof SimpleJpaQuery, is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsANativeJpaQueryFromAnnotation() throws Exception {
|
||||
|
||||
RepositoryQuery query = createJpaQuery(SampleRepository.class.getMethod("findNativeByLastname", String.class));
|
||||
assertThat(query instanceof NativeJpaQuery, is(true));
|
||||
}
|
||||
|
||||
private RepositoryQuery createJpaQuery(Method method) {
|
||||
|
||||
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
|
||||
new SimpleJpaQuery(queryMethod, em);
|
||||
return JpaQueryFactory.INSTANCE.fromQueryAnnotation(queryMethod, em);
|
||||
}
|
||||
|
||||
interface SampleRepository {
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.data.repository.query.parser.Part.Type;
|
||||
* Unit tests for {@link StringQuery}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class StringQueryUnitTests {
|
||||
|
||||
@@ -41,7 +42,7 @@ public class StringQueryUnitTests {
|
||||
StringQuery query = new StringQuery(source);
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is(source));
|
||||
assertThat(query.getQueryString(), is(source));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(1));
|
||||
@@ -57,7 +58,7 @@ public class StringQueryUnitTests {
|
||||
StringQuery query = new StringQuery("select u from User u where u.firstname like %?1% or u.lastname like %?2");
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
|
||||
assertThat(query.getQueryString(), is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(2));
|
||||
@@ -79,7 +80,7 @@ public class StringQueryUnitTests {
|
||||
StringQuery query = new StringQuery("select u from User u where u.firstname like %:firstname");
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is("select u from User u where u.firstname like :firstname"));
|
||||
assertThat(query.getQueryString(), is("select u from User u where u.firstname like :firstname"));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(1));
|
||||
|
||||
Reference in New Issue
Block a user