1 /*
2  * Copyright 2013-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.jpa.repository.query;
17
18 import javax.annotation.Nullable;
19 import javax.persistence.EntityManager;
20 import javax.persistence.Query;
21 import javax.persistence.Tuple;
22
23 import org.springframework.data.repository.query.Parameters;
24 import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
25 import org.springframework.data.repository.query.RepositoryQuery;
26 import org.springframework.data.repository.query.ReturnedType;
27 import org.springframework.expression.spel.standard.SpelExpressionParser;
28
29 /**
30  * {@link RepositoryQuery} implementation that inspects a {@link org.springframework.data.repository.query.QueryMethod}
31  * for the existence of an {@link org.springframework.data.jpa.repository.Query} annotation and creates a JPA native
32  * {@link Query} from it.
33  *
34  * @author Thomas Darimont
35  * @author Oliver Gierke
36  * @author Jens Schauder
37  */

38 final class NativeJpaQuery extends AbstractStringBasedJpaQuery {
39
40     /**
41      * Creates a new {@link NativeJpaQuery} encapsulating the query annotated on the given {@link JpaQueryMethod}.
42      *
43      * @param method must not be {@literal null}.
44      * @param em must not be {@literal null}.
45      * @param queryString must not be {@literal null} or empty.
46      * @param evaluationContextProvider
47      */

48     public NativeJpaQuery(JpaQueryMethod method, EntityManager em, String queryString,
49             QueryMethodEvaluationContextProvider evaluationContextProvider, SpelExpressionParser parser) {
50
51         super(method, em, queryString, evaluationContextProvider, parser);
52
53         Parameters<?, ?> parameters = method.getParameters();
54
55         if (parameters.hasSortParameter() && !queryString.contains("#sort")) {
56             throw new InvalidJpaQueryMethodException("Cannot use native queries with dynamic sorting in method " + method);
57         }
58     }
59
60     /*
61      * (non-Javadoc)
62      * @see org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery#createJpaQuery(java.lang.String)
63      */

64     @Override
65     protected Query createJpaQuery(String queryString, ReturnedType returnedType) {
66
67         EntityManager em = getEntityManager();
68         Class<?> type = getTypeToQueryFor(returnedType);
69
70         return type == null ? em.createNativeQuery(queryString) : em.createNativeQuery(queryString, type);
71     }
72
73     @Nullable
74     private Class<?> getTypeToQueryFor(ReturnedType returnedType) {
75
76         Class<?> result = getQueryMethod().isQueryForEntity() ? returnedType.getDomainType() : null;
77
78         if (this.getQuery().hasConstructorExpression() || this.getQuery().isDefaultProjection()) {
79             return result;
80         }
81
82         return returnedType.isProjecting() && !getMetamodel().isJpaManaged(returnedType.getReturnedType()) //
83                 ? Tuple.class
84                 : result;
85     }
86 }
87