Skip to content
Open
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-relational-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-2121-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-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-relational-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-2121-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
29 changes: 27 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-2121-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-2121-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -167,6 +167,18 @@

<!-- Test dependencies -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down Expand Up @@ -194,6 +206,19 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
TypeReference.of("org.springframework.core.DecoratingProxy"));

hints.reflection().registerType(TypeReference.of("org.postgresql.jdbc.TypeInfoCache"),
MemberCategory.PUBLIC_CLASSES);
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);

for (Class<?> simpleType : JdbcPostgresDialect.INSTANCE.simpleTypes()) {
hints.reflection().registerType(TypeReference.of(simpleType), MemberCategory.PUBLIC_CLASSES);
hints.reflection().registerType(TypeReference.of(simpleType), MemberCategory.INVOKE_DECLARED_METHODS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.relational.core.query.Query;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;

/**
Expand Down Expand Up @@ -342,4 +343,16 @@ public interface JdbcAggregateOperations {
*/
DataAccessStrategy getDataAccessStrategy();

/**
* Return a {@link RowMapper} that can map rows of a {@link java.sql.ResultSet} to instances of the specified
* {@link Class type}. The row mapper supports entity callbacks and lifecycle events if enabled and configured on the
* underlying template instance.
*
* @param type type of the entity to map.
* @return a row mapper for the given type.
* @param <T>
* @since 4.0
*/
<T> RowMapper<? extends T> getRowMapper(Class<T> type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.jdbc.core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -36,7 +38,9 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.EntityRowMapper;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.EntityLifecycleEventDelegate;
Expand All @@ -55,6 +59,7 @@
import org.springframework.data.relational.core.mapping.event.*;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -83,6 +88,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations, Applicati
private final JdbcConverter converter;

private @Nullable EntityCallbacks entityCallbacks;
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;

/**
* Creates a new {@link JdbcAggregateTemplate} given {@link RelationalMappingContext} and {@link DataAccessStrategy}.
Expand Down Expand Up @@ -186,6 +192,23 @@ public void setEntityLifecycleEventsEnabled(boolean enabled) {
this.eventDelegate.setEventsEnabled(enabled);
}

/**
* Return a {@link RowMapper} to map results for {@link Class type}.
*
* @param type must not be {@literal null}.
* @return a row mapper to map results for {@link Class type}.
* @param <T> return type.
* @since 4.0
*/
@Override
@SuppressWarnings("unchecked")
public <T> RowMapper<? extends T> getRowMapper(Class<T> type) {

RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(type);

return new LifecycleEntityRowMapper<T>((RelationalPersistentEntity<T>) entity);
}

@Override
public <T> T save(T instance) {

Expand Down Expand Up @@ -741,4 +764,29 @@ private interface AggregateChangeCreator<T> {
RootAggregateChange<T> createAggregateChange(T instance);
}

class LifecycleEntityRowMapper<T> extends EntityRowMapper<T> {

public LifecycleEntityRowMapper(RelationalPersistentEntity<T> entity) {
super(entity, converter);
}

@Override
public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {

T object = super.mapRow(resultSet, rowNumber);

if (object != null) {

eventDelegate.publishEvent(() -> new AfterConvertEvent<>(object));

if (entityCallbacks != null) {
return entityCallbacks.callback(AfterConvertCallback.class, object);
}
}

return object;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ public class EntityRowMapper<T> implements RowMapper<T> {
private final JdbcConverter converter;
private final Identifier identifier;

private EntityRowMapper(TypeInformation<T> typeInformation, JdbcConverter converter, Identifier identifier) {

this.typeInformation = typeInformation;
this.converter = converter;
this.identifier = identifier;
}

@SuppressWarnings("unchecked")
public EntityRowMapper(AggregatePath path, JdbcConverter converter, Identifier identifier) {
this(((RelationalPersistentEntity<T>) path.getRequiredLeafEntity()).getTypeInformation(), converter, identifier);
Expand All @@ -57,6 +50,13 @@ public EntityRowMapper(RelationalPersistentEntity<T> entity, JdbcConverter conve
this(entity.getTypeInformation(), converter, Identifier.empty());
}

private EntityRowMapper(TypeInformation<T> typeInformation, JdbcConverter converter, Identifier identifier) {

this.typeInformation = typeInformation;
this.converter = converter;
this.identifier = identifier;
}

@Override
public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@
*/
public interface QueryMappingConfiguration {

@Nullable
default <T> RowMapper<? extends T> getRowMapper(Class<T> type) {
return null;
}
/**
* Returns the {@link RowMapper} to be used for the given type or {@literal null} if no specific mapper is configured.
*
* @param type
* @return
* @param <T>
*/
<T> @Nullable RowMapper<? extends T> getRowMapper(Class<T> type);

/**
* An immutable empty instance that will return {@literal null} for all arguments.
*/
QueryMappingConfiguration EMPTY = new QueryMappingConfiguration() {

@Override
public <T> RowMapper<? extends T> getRowMapper(Class<T> type) {
public <T> @Nullable RowMapper<? extends T> getRowMapper(Class<T> type) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.jdbc.repository.aot;

import java.util.LinkedHashMap;
import java.util.Map;

import org.jspecify.annotations.Nullable;

import org.springframework.data.repository.aot.generate.QueryMetadata;

/**
* Value object capturing queries used for repository query methods.
*
* @author Mark Paluch
* @since 4.0
*/
record AotQueries(AotQuery result, @Nullable AotQuery count) {

/**
* Factory method to create an {@link AotQueries} instance with a single query.
*
* @param query
* @return
*/
public static AotQueries create(AotQuery query) {
return new AotQueries(query, null);
}

/**
* Factory method to create an {@link AotQueries} instance with an entity- and a count query.
*
* @param query
* @param count
* @return
*/
public static AotQueries create(AotQuery query, AotQuery count) {
return new AotQueries(query, count);
}

public QueryMetadata toMetadata() {
return new AotQueryMetadata();
}

/**
* String and Named Query-based {@link QueryMetadata}.
*/
private class AotQueryMetadata implements QueryMetadata {

@Override
public Map<String, Object> serialize() {

Map<String, Object> serialized = new LinkedHashMap<>();

if (result() instanceof StringAotQuery sq) {
serialized.put("query", sq.getQueryString());
}

if (result() instanceof StringAotQuery.NamedStringAotQuery nsq) {
serialized.put("name", nsq.getQueryName());
}

if (count() instanceof StringAotQuery sq) {
serialized.put("count-query", sq.getQueryString());
}

if (count() instanceof StringAotQuery.NamedStringAotQuery nsq) {
serialized.put("count-name", nsq.getQueryName());
}

return serialized;
}

}

}
Loading
Loading