Skip to content

Commit 95e0637

Browse files
committed
Verify that single-parameter DTOs work with JPQL queries.
Closes #1869.
1 parent b57eb85 commit 95e0637

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727
import org.junit.jupiter.api.extension.ExtendWith;
28-
2928
import org.springframework.beans.factory.annotation.Autowired;
3029
import org.springframework.dao.InvalidDataAccessApiUsageException;
3130
import org.springframework.data.domain.Page;
@@ -288,4 +287,9 @@ void rejectsStreamExecutionIfNoSurroundingTransactionActive() {
288287
void executesNamedQueryWithConstructorExpression() {
289288
userRepository.findByNamedQueryWithConstructorExpression();
290289
}
290+
291+
@Test // GH-1869
292+
void executesNamedQueryWithSingleConstructorExpression() {
293+
userRepository.findByNamedQueryWithSingleConstructor("Dave");
294+
}
291295
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2018-2022 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+
/**
19+
* Verify that single argument DTOs work with JPQL new object queries.
20+
*
21+
* @author Greg Turnquist
22+
* @see https://github.com/spring-projects/spring-data-jpa/issues/1869
23+
*/
24+
class FirstNameDto {
25+
26+
private String firstname;
27+
28+
public FirstNameDto(String firstname) {
29+
this.firstname = firstname;
30+
}
31+
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java

+4
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ Page<User> findAllOrderedBySpecialNameMultipleParams(@Param("name") String name,
607607
// DATAJPA-1334
608608
List<NameOnlyDto> findByNamedQueryWithConstructorExpression();
609609

610+
// GH-1869
611+
@Query("SELECT new org.springframework.data.jpa.repository.sample.FirstNameDto(u.firstname) from User u where u.firstname = :firstname")
612+
List<FirstNameDto> findByNamedQueryWithSingleConstructor(@Param("firstname") String firstname);
613+
610614
// DATAJPA-1519
611615
@Query("select u from User u where u.lastname like %?#{escape([0])}% escape ?#{escapeCharacter()}")
612616
List<User> findContainingEscaped(String namePart);

0 commit comments

Comments
 (0)