Browse Source

Exclude DTO types without custom construction from DTO constructor rewriting.

We now verify that we can actually express a valid constructor expression before rewriting queries to use constructor expressions.

See #3929
pull/3940/head
Mark Paluch 7 months ago
parent
commit
caa4704cd0
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 5
      spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java
  2. 41
      spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Country.java
  3. 4
      spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Customer.java
  4. 21
      spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java
  5. 15
      spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/SimpleJpaQueryUnitTests.java
  6. 4
      spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithEmbeddedId.java

5
spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java

@ -145,7 +145,7 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery { @@ -145,7 +145,7 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
ReturnedType getReturnedType(ResultProcessor processor) {
ReturnedType returnedType = processor.getReturnedType();
Class<?> returnedJavaType = processor.getReturnedType().getReturnedType();
Class<?> returnedJavaType = returnedType.getReturnedType();
if (!returnedType.isProjecting() || returnedJavaType.isInterface() || query.isNativeQuery()) {
return returnedType;
@ -157,7 +157,8 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery { @@ -157,7 +157,8 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
return returnedType;
}
if ((known != null && !known) || returnedJavaType.isArray() || getMetamodel().isJpaManaged(returnedJavaType)) {
if ((known != null && !known) || returnedJavaType.isArray() || getMetamodel().isJpaManaged(returnedJavaType)
|| !returnedType.needsCustomConstruction()) {
if (known == null) {
knownProjections.put(returnedJavaType, false);
}

41
spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Country.java

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
/*
* Copyright 2025 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
*
* https://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.domain.sample;
/**
* @author Mark Paluch
*/
public class Country {
private final String code;
// workaround to avoid DTO projections as needsCustomConstruction is false.
private Country(Country other) {
this.code = other.code;
}
private Country(String code) {
this.code = code;
}
public static Country of(String code) {
return new Country(code);
}
public String getCode() {
return code;
}
}

4
spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Customer.java

@ -25,7 +25,7 @@ import jakarta.persistence.Id; @@ -25,7 +25,7 @@ import jakarta.persistence.Id;
@Entity
public class Customer {
@Id Long id;
@Id Long id;
String name;
String name;
}

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

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
*/
package org.springframework.data.jpa.repository;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;
import jakarta.persistence.EntityManager;
@ -25,6 +25,7 @@ import java.util.List; @@ -25,6 +25,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
@ -115,6 +116,24 @@ class RepositoryWithCompositeKeyTests { @@ -115,6 +116,24 @@ class RepositoryWithCompositeKeyTests {
assertThat(persistedEmp.getDepartment().getName()).isEqualTo(dep.getName());
}
@Test // GH-3929
void shouldReturnIdentifiers() {
EmbeddedIdExampleDepartment dep = new EmbeddedIdExampleDepartment();
dep.setName("TestDepartment");
dep.setDepartmentId(-1L);
EmbeddedIdExampleEmployee emp = new EmbeddedIdExampleEmployee();
emp.setDepartment(dep);
emp.setEmployeePk(new EmbeddedIdExampleEmployeePK(1L, 2L));
emp = employeeRepositoryWithEmbeddedId.save(emp);
List<EmbeddedIdExampleEmployeePK> identifiers = employeeRepositoryWithEmbeddedId.findIdentifiers();
assertThat(identifiers).hasSize(1).contains(emp.getEmployeePk());
}
@Test // DATAJPA-472, DATAJPA-912
void shouldSupportFindAllWithPageableAndEntityWithIdClass() {

15
spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/SimpleJpaQueryUnitTests.java

@ -45,6 +45,7 @@ import org.springframework.data.domain.Page; @@ -45,6 +45,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.sample.Country;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.provider.QueryExtractor;
import org.springframework.data.jpa.repository.NativeQuery;
@ -325,6 +326,17 @@ class SimpleJpaQueryUnitTests { @@ -325,6 +326,17 @@ class SimpleJpaQueryUnitTests {
assertThatIllegalArgumentException().isThrownBy(() -> createJpaQuery(illegalMethod));
}
@Test // GH-3929
void doesNotRewriteQueryForDtoWithMultipleConstructors() throws Exception {
AbstractStringBasedJpaQuery jpaQuery = (AbstractStringBasedJpaQuery) createJpaQuery(
SampleRepository.class.getMethod("justCountries"));
String queryString = createQuery(jpaQuery);
assertThat(queryString).startsWith("select u.country from User u");
}
@Test // DATAJPA-1163
void resolvesExpressionInCountQuery() throws Exception {
@ -408,6 +420,9 @@ class SimpleJpaQueryUnitTests { @@ -408,6 +420,9 @@ class SimpleJpaQueryUnitTests {
@Query("select r.name from User u LEFT JOIN FETCH u.roles r")
Collection<UnrelatedType> projectWithJoinPaths();
@Query("select u.country from User u")
Collection<Country> justCountries();
@Query(value = "select u from #{#entityName} u", countQuery = "select count(u.id) from #{#entityName} u")
List<User> findAllWithExpressionInCountQuery(Pageable pageable);

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

@ -21,6 +21,7 @@ import org.springframework.context.annotation.Lazy; @@ -21,6 +21,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.domain.sample.EmbeddedIdExampleEmployee;
import org.springframework.data.jpa.domain.sample.EmbeddedIdExampleEmployeePK;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import com.querydsl.core.types.OrderSpecifier;
@ -40,6 +41,9 @@ public interface EmployeeRepositoryWithEmbeddedId @@ -40,6 +41,9 @@ public interface EmployeeRepositoryWithEmbeddedId
@Override
List<EmbeddedIdExampleEmployee> findAll(Predicate predicate, OrderSpecifier<?>... orders);
@Query("select e.employeePk from EmbeddedIdExampleEmployee e")
List<EmbeddedIdExampleEmployeePK> findIdentifiers();
// DATAJPA-920
boolean existsByName(String name);
}

Loading…
Cancel
Save