File tree Expand file tree Collapse file tree 4 files changed +47
-3
lines changed
main/java/org/springframework/data/jpa/repository/query
test/java/org/springframework/data/jpa/repository Expand file tree Collapse file tree 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) {
9393 return result ;
9494 }
9595
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 ;
99106 }
100107}
Original file line number Diff line number Diff line change 6767import org .springframework .data .jpa .domain .sample .Role ;
6868import org .springframework .data .jpa .domain .sample .SpecialUser ;
6969import org .springframework .data .jpa .domain .sample .User ;
70+ import org .springframework .data .jpa .repository .sample .NameOnlyRecord ;
7071import org .springframework .data .jpa .repository .sample .SampleEvaluationContextExtension .SampleSecurityContextHolder ;
7172import org .springframework .data .jpa .repository .sample .UserRepository ;
7273import org .springframework .data .jpa .repository .sample .UserRepository .NameOnly ;
@@ -2979,6 +2980,19 @@ void supportsInterfaceProjectionsWithNativeQueries() {
29792980 assertThat (result .getLastname ()).isEqualTo (user .getLastname ());
29802981 }
29812982
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+
29822996 @ Test // DATAJPA-1248
29832997 void supportsProjectionsWithNativeQueriesAndCamelCaseProperty () {
29842998
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 @@ -561,6 +561,9 @@ List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntity
561561 @ Query (value = "SELECT firstname, lastname FROM SD_User WHERE id = ?1" , nativeQuery = true )
562562 NameOnly findByNativeQuery (Integer id );
563563
564+ @ NativeQuery ("SELECT firstname, lastname FROM SD_User WHERE id = ?1" )
565+ NameOnlyRecord findRecordProjectionByNativeQuery (Integer id );
566+
564567 // GH-3155
565568 @ NativeQuery (value = "SELECT emailaddress, secondary_email_address FROM SD_User WHERE id = ?1" ,
566569 sqlResultSetMapping = "emailDto" )
You can’t perform that action at this time.
0 commit comments