Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4029-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4029-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4029-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4029-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4029-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -16,7 +16,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.x-GH-4029-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* Factory for {@link AotQueries}.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 4.0
*/
class QueriesFactory {
Expand Down Expand Up @@ -123,7 +124,7 @@ public AotQueries createQueries(RepositoryInformation repositoryInformation, Ret
QueryEnhancerSelector selector, MergedAnnotation<Query> query, JpaQueryMethod queryMethod) {

if (query.isPresent() && StringUtils.hasText(query.getString("value"))) {
return buildStringQuery(repositoryInformation.getDomainType(), returnedType, selector, query, queryMethod);
return buildStringQuery(returnedType, selector, query, queryMethod);
}

String queryName = queryMethod.getNamedQueryName();
Expand All @@ -138,10 +139,10 @@ private boolean hasNamedQuery(ReturnedType returnedType, String queryName) {
return namedQueries.hasQuery(queryName) || getNamedQuery(returnedType, queryName) != null;
}

private AotQueries buildStringQuery(Class<?> domainType, ReturnedType returnedType, QueryEnhancerSelector selector,
private AotQueries buildStringQuery(ReturnedType returnedType, QueryEnhancerSelector selector,
MergedAnnotation<Query> query, JpaQueryMethod queryMethod) {

UnaryOperator<String> operator = s -> s.replaceAll("#\\{#entityName}", domainType.getSimpleName());
UnaryOperator<String> operator = s -> s.replaceAll("#\\{#entityName}", queryMethod.getEntityInformation().getEntityName());
boolean isNative = query.getBoolean("nativeQuery");
Function<String, DeclaredQuery> queryFunction = isNative ? DeclaredQuery::nativeQuery : DeclaredQuery::jpqlQuery;
queryFunction = operator.andThen(queryFunction);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2025-present 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.repository.aot;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import jakarta.persistence.EntityManagerFactory;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.query.JpaEntityMetadata;
import org.springframework.data.jpa.repository.query.JpaQueryMethod;
import org.springframework.data.jpa.repository.query.QueryEnhancerSelector;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.config.RepositoryConfigurationSource;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.query.ReturnedType;

/**
* Unit tests for {@link QueriesFactory}.
*
* @author Christoph Strobl
*/
class QueriesFactoryUnitTests {

QueriesFactory factory;

@BeforeEach
void setUp() {

RepositoryConfigurationSource configSource = Mockito.mock(RepositoryConfigurationSource.class);
EntityManagerFactory entityManagerFactory = Mockito.mock(EntityManagerFactory.class);

factory = new QueriesFactory(configSource, entityManagerFactory, this.getClass().getClassLoader());
}

@Test // GH-4029
@SuppressWarnings({ "rawtypes", "unchecked" })
void stringQueryShouldResolveEntityNameFromJakartaAnnotationIfPresent() {

RepositoryInformation repositoryInformation = Mockito.mock(RepositoryInformation.class);
JpaEntityMetadata<?> entityMetadata = Mockito.mock(JpaEntityInformation.class);
when(entityMetadata.getEntityName()).thenReturn("CustomNamed");

MergedAnnotation<Query> queryAnnotation = Mockito.mock(MergedAnnotation.class);
when(queryAnnotation.isPresent()).thenReturn(true);
when(queryAnnotation.getString(eq("value"))).thenReturn("select t from #{#entityName} t");
when(queryAnnotation.getBoolean(eq("nativeQuery"))).thenReturn(false);
when(queryAnnotation.getString("countQuery")).thenReturn("select count(t) from #{#entityName} t");

JpaQueryMethod queryMethod = Mockito.mock(JpaQueryMethod.class);
when(queryMethod.getEntityInformation()).thenReturn((JpaEntityMetadata) entityMetadata);

AotQueries generatedQueries = factory.createQueries(repositoryInformation,
ReturnedType.of(Object.class, Object.class, new SpelAwareProxyProjectionFactory()),
QueryEnhancerSelector.DEFAULT_SELECTOR, queryAnnotation, queryMethod);

assertThat(generatedQueries.result()).asInstanceOf(InstanceOfAssertFactories.type(StringAotQuery.class))
.extracting(StringAotQuery::getQueryString).isEqualTo("select t from CustomNamed t");
assertThat(generatedQueries.count()).asInstanceOf(InstanceOfAssertFactories.type(StringAotQuery.class))
.extracting(StringAotQuery::getQueryString).isEqualTo("select count(t) from CustomNamed t");
}
}