Skip to content

Commit 8046171

Browse files
committed
Provide a complete implementation for the deprecated DataAccessStrategy#insert method signature.
1 parent 0214105 commit 8046171

File tree

7 files changed

+55
-29
lines changed

7 files changed

+55
-29
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public CascadingDataAccessStrategy(List<DataAccessStrategy> strategies) {
4747
this.strategies = new ArrayList<>(strategies);
4848
}
4949

50+
@Override
51+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
52+
return collect(das -> das.insert(instance, domainType, identifier));
53+
}
54+
5055
/*
5156
* (non-Javadoc)
5257
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, org.springframework.data.jdbc.core.ParentKeys)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public interface DataAccessStrategy extends RelationResolver {
5757
*/
5858
@Nullable
5959
@Deprecated
60-
default <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
61-
return insert(instance, domainType, identifier, IdValueSource.GENERATED);
62-
}
60+
<T> Object insert(T instance, Class<T> domainType, Identifier identifier);
6361

6462
/**
6563
* Inserts the data of a single entity. Referenced entities don't get handled.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, Relation
9494
this.insertStrategyFactory = insertStrategyFactory;
9595
}
9696

97+
@Override
98+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
99+
100+
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(domainType);
101+
return insert(instance, domainType, identifier, IdValueSource.forInstance(instance, persistentEntity));
102+
}
103+
97104
/*
98105
* (non-Javadoc)
99106
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class DelegatingDataAccessStrategy implements DataAccessStrategy {
4040

4141
private DataAccessStrategy delegate;
4242

43+
@Override
44+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
45+
return delegate.insert(instance, domainType, identifier);
46+
}
47+
4348
/*
4449
* (non-Javadoc)
4550
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, org.springframework.data.jdbc.core.ParentKeys)

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ public void setNamespaceStrategy(NamespaceStrategy namespaceStrategy) {
147147
this.namespaceStrategy = namespaceStrategy;
148148
}
149149

150+
@Override
151+
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
152+
153+
MyBatisContext myBatisContext = new MyBatisContext(identifier, instance, domainType);
154+
sqlSession().insert(namespace(domainType) + ".insert", myBatisContext);
155+
156+
return myBatisContext.getId();
157+
}
158+
150159
/*
151160
* (non-Javadoc)
152161
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, Identifier, boolean)

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/IdValueSource.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.relational.core.conversion;
1717

18+
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
19+
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
20+
1821
/**
1922
* Enumeration describing the source of a value for an id property.
2023
*
@@ -36,5 +39,27 @@ public enum IdValueSource {
3639
/**
3740
* There is no id property, and therefore no id value source.
3841
*/
39-
NONE
42+
NONE;
43+
44+
/**
45+
* Returns the appropriate {@link IdValueSource} for the instance: {@link IdValueSource#NONE} when the entity has no
46+
* id property, {@link IdValueSource#PROVIDED} when the value of the id property is not null and when it is a
47+
* primitive type, not zero, and {@link IdValueSource#GENERATED} otherwise.
48+
*/
49+
public static <T> IdValueSource forInstance(Object instance, RelationalPersistentEntity<T> persistentEntity) {
50+
51+
Object idValue = persistentEntity.getIdentifierAccessor(instance).getIdentifier();
52+
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
53+
if (idProperty == null) {
54+
return IdValueSource.NONE;
55+
}
56+
boolean idPropertyValueIsSet = idValue != null && //
57+
(idProperty.getType() != int.class || !idValue.equals(0)) //
58+
&& (idProperty.getType() != long.class || !idValue.equals(0L));
59+
if (idPropertyValueIsSet) {
60+
return IdValueSource.PROVIDED;
61+
} else {
62+
return IdValueSource.GENERATED;
63+
}
64+
}
4065
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class WritingContext {
5555
this.root = root;
5656
this.entity = aggregateChange.getEntity();
5757
this.entityType = aggregateChange.getEntityType();
58-
this.rootIdValueSource = idValueSource(root, context.getRequiredPersistentEntity(aggregateChange.getEntityType()));
58+
this.rootIdValueSource = IdValueSource.forInstance(root, context.getRequiredPersistentEntity(aggregateChange.getEntityType()));
5959
this.paths = context.findPersistentPropertyPaths(entityType, (p) -> p.isEntity() && !p.isEmbedded());
6060
}
6161

@@ -145,7 +145,7 @@ private List<? extends DbAction<?>> insertAll(PersistentPropertyPath<RelationalP
145145
} else {
146146
instance = node.getValue();
147147
}
148-
IdValueSource idValueSource = idValueSource(instance, persistentEntity);
148+
IdValueSource idValueSource = IdValueSource.forInstance(instance, persistentEntity);
149149
DbAction.Insert<Object> insert = new DbAction.Insert<>(instance, path, parentAction, qualifiers, idValueSource);
150150
inserts.add(insert);
151151
previousActions.put(node, insert);
@@ -303,28 +303,5 @@ private List<PathNode> createNodes(PersistentPropertyPath<RelationalPersistentPr
303303

304304
return nodes;
305305
}
306-
307-
/**
308-
* Returns the appropriate {@link IdValueSource} for the instance: {@link IdValueSource#NONE} when the entity has no
309-
* id property, {@link IdValueSource#PROVIDED} when the value of the id property is not null and when it is a
310-
* primitive type, not zero, and {@link IdValueSource#GENERATED} otherwise.
311-
*/
312-
private static <S> IdValueSource idValueSource(Object instance,
313-
RelationalPersistentEntity<S> persistentEntity) {
314-
315-
Object idValue = persistentEntity.getIdentifierAccessor(instance).getIdentifier();
316-
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
317-
if (idProperty == null) {
318-
return IdValueSource.NONE;
319-
}
320-
boolean idPropertyValueIsSet = idValue != null && //
321-
(idProperty.getType() != int.class || !idValue.equals(0)) //
322-
&& (idProperty.getType() != long.class || !idValue.equals(0L));
323-
if (idPropertyValueIsSet) {
324-
return IdValueSource.PROVIDED;
325-
} else {
326-
return IdValueSource.GENERATED;
327-
}
328-
}
329306

330307
}

0 commit comments

Comments
 (0)