Skip to content

Commit d848ad7

Browse files
committed
DATAJDBC-326 - Applied feedback from review.
ParentKeys gets renamed to Identifier and moved to spring-data-relational. This facilitates reuse beyond just references to parents. The factory methods moved to BasicJdbcConverter.
1 parent cfd7895 commit d848ad7

File tree

11 files changed

+214
-181
lines changed

11 files changed

+214
-181
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/CascadingDataAccessStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.function.Consumer;
2222
import java.util.function.Function;
2323

24+
import org.springframework.data.relational.domain.Identifier;
2425
import org.springframework.data.mapping.PersistentPropertyPath;
2526
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2627

@@ -52,8 +53,8 @@ public <T> Object insert(T instance, Class<T> domainType, Map<String, Object> ad
5253
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, org.springframework.data.jdbc.core.ParentKeys)
5354
*/
5455
@Override
55-
public <T> Object insert(T instance, Class<T> domainType, ParentKeys parentKeys) {
56-
return collect(das -> das.insert(instance, domainType, parentKeys));
56+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
57+
return collect(das -> das.insert(instance, domainType, identifier));
5758
}
5859

5960
/*

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DataAccessStrategy.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Map;
1919

20+
import org.springframework.data.relational.domain.Identifier;
2021
import org.springframework.data.mapping.PersistentPropertyPath;
2122
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2223
import org.springframework.lang.Nullable;
@@ -40,7 +41,7 @@ public interface DataAccessStrategy {
4041
* @param <T> the type of the instance.
4142
* @return the id generated by the database if any.
4243
*
43-
* @deprecated use {@link #insert(Object, Class, ParentKeys)} instead.
44+
* @deprecated use {@link #insert(Object, Class, Identifier)} instead.
4445
*/
4546
@Deprecated
4647
<T> Object insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters);
@@ -51,13 +52,13 @@ public interface DataAccessStrategy {
5152
*
5253
* @param instance the instance to be stored. Must not be {@code null}.
5354
* @param domainType the type of the instance. Must not be {@code null}.
54-
* @param parentKeys information about data that needs to be considered for the insert but which is not part of the entity.
55+
* @param identifier information about data that needs to be considered for the insert but which is not part of the entity.
5556
* Namely references back to a parent entity and key/index columns for entities that are stored in a {@link Map} or {@link java.util.List}.
5657
* @param <T> the type of the instance.
5758
* @return the id generated by the database if any.
5859
*/
59-
default <T> Object insert(T instance, Class<T> domainType, ParentKeys parentKeys){
60-
return insert(instance, domainType, parentKeys.getParametersByName());
60+
default <T> Object insert(T instance, Class<T> domainType, Identifier identifier){
61+
return insert(instance, domainType, identifier.getParametersByName());
6162
}
6263

6364
/**

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.dao.DataRetrievalFailureException;
3030
import org.springframework.dao.EmptyResultDataAccessException;
3131
import org.springframework.dao.InvalidDataAccessApiUsageException;
32+
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
3233
import org.springframework.data.jdbc.support.JdbcUtil;
3334
import org.springframework.data.mapping.PersistentPropertyAccessor;
3435
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -37,6 +38,7 @@
3738
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3839
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3940
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
41+
import org.springframework.data.relational.domain.Identifier;
4042
import org.springframework.data.util.ClassTypeInformation;
4143
import org.springframework.jdbc.core.RowMapper;
4244
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@@ -87,23 +89,24 @@ public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, Relation
8789
*/
8890
@Override
8991
public <T> Object insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {
90-
return insert(instance, domainType, ParentKeys.fromNamedValues(additionalParameters));
92+
return insert(instance, domainType, BasicJdbcConverter.fromNamedValues(additionalParameters));
9193
}
9294

9395
/*
9496
* (non-Javadoc)
9597
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
9698
*/
9799
@Override
98-
public <T> Object insert(T instance, Class<T> domainType, ParentKeys parentKeys) {
100+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
99101

100102
KeyHolder holder = new GeneratedKeyHolder();
101103
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
102104

103105
Map<String, Object> parameters = new LinkedHashMap<>();
104-
for (ParentKeys.ParentKey parameter : parentKeys.getParameters()) {
105-
parameters.put(parameter.getName(), converter.writeValue(parameter.getValue(), ClassTypeInformation.from(parameter.getTargetType())));
106-
}
106+
identifier.forEach(identifierValue -> {
107+
parameters.put(identifierValue.getName(),
108+
converter.writeValue(identifierValue.getValue(), ClassTypeInformation.from(identifierValue.getTargetType())));
109+
});
107110

108111
MapSqlParameterSource parameterSource = getPropertyMap(instance, persistentEntity, "");
109112

@@ -295,7 +298,8 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
295298
return result;
296299
}
297300

298-
private <S, T> MapSqlParameterSource getPropertyMap(final S instance, RelationalPersistentEntity<S> persistentEntity, String prefix) {
301+
private <S, T> MapSqlParameterSource getPropertyMap(final S instance, RelationalPersistentEntity<S> persistentEntity,
302+
String prefix) {
299303

300304
MapSqlParameterSource parameters = new MapSqlParameterSource();
301305

@@ -307,23 +311,26 @@ private <S, T> MapSqlParameterSource getPropertyMap(final S instance, Relational
307311
return;
308312
}
309313

310-
if(property.isEmbedded()){
314+
if (property.isEmbedded()) {
311315

312316
Object value = propertyAccessor.getProperty(property);
313-
final RelationalPersistentEntity<?> embeddedEntity = context.getPersistentEntity(property.getType());
314-
final MapSqlParameterSource additionalParameters = getPropertyMap((T)value, (RelationalPersistentEntity<T>) embeddedEntity, prefix + property.getEmbeddedPrefix());
317+
final RelationalPersistentEntity<?> embeddedEntity = context.getPersistentEntity(property.getType());
318+
final MapSqlParameterSource additionalParameters = getPropertyMap((T) value,
319+
(RelationalPersistentEntity<T>) embeddedEntity, prefix + property.getEmbeddedPrefix());
315320
parameters.addValues(additionalParameters.getValues());
316321
} else {
317322

318323
Object value = propertyAccessor.getProperty(property);
319324
Object convertedValue = convertForWrite(property, value);
320325

321-
parameters.addValue(prefix + property.getColumnName(), convertedValue, JdbcUtil.sqlTypeFor(property.getColumnType()));
326+
parameters.addValue(prefix + property.getColumnName(), convertedValue,
327+
JdbcUtil.sqlTypeFor(property.getColumnType()));
322328
}
323329
});
324330

325331
return parameters;
326332
}
333+
327334
@Nullable
328335
private Object convertForWrite(RelationalPersistentProperty property, @Nullable Object value) {
329336

@@ -340,9 +347,8 @@ private Object convertForWrite(RelationalPersistentProperty property, @Nullable
340347

341348
String typeName = JDBCType.valueOf(JdbcUtil.sqlTypeFor(componentType)).getName();
342349

343-
return operations.getJdbcOperations().execute(
344-
(Connection c) -> c.createArrayOf(typeName, (Object[]) convertedValue)
345-
);
350+
return operations.getJdbcOperations()
351+
.execute((Connection c) -> c.createArrayOf(typeName, (Object[]) convertedValue));
346352
}
347353

348354
@SuppressWarnings("unchecked")
@@ -367,22 +373,22 @@ private static <S, ID> boolean isIdPropertyNullOrScalarZero(@Nullable ID idValue
367373
@Nullable
368374
private <S> Object getIdFromHolder(KeyHolder holder, RelationalPersistentEntity<S> persistentEntity) {
369375

370-
try {
371-
// MySQL just returns one value with a special name
372-
return holder.getKey();
373-
} catch (DataRetrievalFailureException | InvalidDataAccessApiUsageException e) {
374-
// Postgres returns a value for each column
376+
try {
377+
// MySQL just returns one value with a special name
378+
return holder.getKey();
379+
} catch (DataRetrievalFailureException | InvalidDataAccessApiUsageException e) {
380+
// Postgres returns a value for each column
375381
// MS SQL Server returns a value that might be null.
376382

377-
Map<String, Object> keys = holder.getKeys();
383+
Map<String, Object> keys = holder.getKeys();
378384

379-
if (keys == null || persistentEntity.getIdProperty() == null) {
380-
return null;
381-
}
385+
if (keys == null || persistentEntity.getIdProperty() == null) {
386+
return null;
387+
}
382388

383-
return keys.get(persistentEntity.getIdColumn());
384-
}
385-
}
389+
return keys.get(persistentEntity.getIdColumn());
390+
}
391+
}
386392

387393
private EntityRowMapper<?> getEntityRowMapper(Class<?> domainType) {
388394
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), context, converter, accessStrategy);

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
package org.springframework.data.jdbc.core;
1717

1818
import lombok.RequiredArgsConstructor;
19-
import lombok.val;
2019

2120
import java.util.Collections;
2221
import java.util.Map;
2322

23+
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
24+
import org.springframework.data.relational.domain.Identifier;
2425
import org.springframework.data.mapping.PersistentPropertyPath;
2526
import org.springframework.data.relational.core.conversion.DbAction;
2627
import org.springframework.data.relational.core.conversion.DbAction.Delete;
@@ -37,7 +38,6 @@
3738
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3839
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3940
import org.springframework.lang.Nullable;
40-
import org.springframework.util.Assert;
4141

4242
/**
4343
* {@link Interpreter} for {@link DbAction}s using a {@link DataAccessStrategy} for performing actual database
@@ -142,14 +142,14 @@ public <T> void interpret(DeleteAllRoot<T> deleteAllRoot) {
142142
accessStrategy.deleteAll(deleteAllRoot.getEntityType());
143143
}
144144

145-
private <T> ParentKeys getParentKeys(DbAction.WithDependingOn<?> action) {
145+
private <T> Identifier getParentKeys(DbAction.WithDependingOn<?> action) {
146146

147147
DbAction.WithEntity<?> dependingOn = action.getDependingOn();
148148

149149
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(dependingOn.getEntityType());
150150

151151
Object identifier = getIdFromEntityDependingOn(dependingOn, persistentEntity);
152-
ParentKeys parentKeys = ParentKeys //
152+
Identifier parentKeys = BasicJdbcConverter //
153153
.forBackReferences(action.getPropertyPath(), identifier);
154154

155155
for (Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object> qualifier : action.getQualifiers().entrySet()) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DelegatingDataAccessStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Map;
1919

20+
import org.springframework.data.relational.domain.Identifier;
2021
import org.springframework.data.mapping.PersistentPropertyPath;
2122
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2223
import org.springframework.util.Assert;
@@ -45,8 +46,8 @@ public <T> Object insert(T instance, Class<T> domainType, Map<String, Object> ad
4546
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, org.springframework.data.jdbc.core.ParentKeys)
4647
*/
4748
@Override
48-
public <T> Object insert(T instance, Class<T> domainType, ParentKeys parentKeys) {
49-
return delegate.insert(instance, domainType, parentKeys);
49+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
50+
return delegate.insert(instance, domainType, identifier);
5051
}
5152

5253
/*

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/ParentKeys.java

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)