File tree 4 files changed +47
-3
lines changed
main/java/org/springframework/data/jpa/repository/query
test/java/org/springframework/data/jpa/repository
4 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -93,8 +93,15 @@ private Class<?> getTypeToQueryFor(ReturnedType returnedType) {
93
93
return result ;
94
94
}
95
95
96
- return returnedType .isProjecting () && !getMetamodel ().isJpaManaged (returnedType .getReturnedType ()) //
97
- ? Tuple .class
98
- : result ;
96
+ if (returnedType .isProjecting ()) {
97
+
98
+ if (returnedType .getReturnedType ().isInterface ()) {
99
+ return Tuple .class ;
100
+ }
101
+
102
+ return returnedType .getReturnedType ();
103
+ }
104
+
105
+ return result ;
99
106
}
100
107
}
Original file line number Diff line number Diff line change 67
67
import org .springframework .data .jpa .domain .sample .Role ;
68
68
import org .springframework .data .jpa .domain .sample .SpecialUser ;
69
69
import org .springframework .data .jpa .domain .sample .User ;
70
+ import org .springframework .data .jpa .repository .sample .NameOnlyRecord ;
70
71
import org .springframework .data .jpa .repository .sample .SampleEvaluationContextExtension .SampleSecurityContextHolder ;
71
72
import org .springframework .data .jpa .repository .sample .UserRepository ;
72
73
import org .springframework .data .jpa .repository .sample .UserRepository .NameOnly ;
@@ -2979,6 +2980,19 @@ void supportsInterfaceProjectionsWithNativeQueries() {
2979
2980
assertThat (result .getLastname ()).isEqualTo (user .getLastname ());
2980
2981
}
2981
2982
2983
+ @ Test // GH-2757
2984
+ void supportsRecordsWithNativeQueries () {
2985
+
2986
+ flushTestUsers ();
2987
+
2988
+ User user = repository .findAll ().get (0 );
2989
+
2990
+ NameOnlyRecord result = repository .findRecordProjectionByNativeQuery (user .getId ());
2991
+
2992
+ assertThat (result .firstname ()).isEqualTo (user .getFirstname ());
2993
+ assertThat (result .lastname ()).isEqualTo (user .getLastname ());
2994
+ }
2995
+
2982
2996
@ Test // DATAJPA-1248
2983
2997
void supportsProjectionsWithNativeQueriesAndCamelCaseProperty () {
2984
2998
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2018-2024 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 .sample ;
17
+
18
+ public record NameOnlyRecord (String firstname , String lastname ) {
19
+
20
+ }
Original file line number Diff line number Diff line change @@ -558,6 +558,9 @@ List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntity
558
558
@ Query (value = "SELECT firstname, lastname FROM SD_User WHERE id = ?1" , nativeQuery = true )
559
559
NameOnly findByNativeQuery (Integer id );
560
560
561
+ @ NativeQuery ("SELECT firstname, lastname FROM SD_User WHERE id = ?1" )
562
+ NameOnlyRecord findRecordProjectionByNativeQuery (Integer id );
563
+
561
564
// GH-3155
562
565
@ NativeQuery (value = "SELECT emailaddress, secondary_email_address FROM SD_User WHERE id = ?1" ,
563
566
sqlResultSetMapping = "emailDto" )
You can’t perform that action at this time.
0 commit comments