domainType);
/**
- * Updates the data of a single entity in the database and enforce optimistic record locking using the {@code previousVersion}
- * property. Referenced entities don't get handled.
+ * Updates the data of a single entity in the database and enforce optimistic record locking using the
+ * {@code previousVersion} property. Referenced entities don't get handled.
*
* The statement will be of the form : {@code UPDATE … SET … WHERE ID = :id and VERSION_COLUMN = :previousVersion }
* and throw an optimistic record locking exception if no rows have been updated.
@@ -87,7 +89,8 @@ default Object insert(T instance, Class domainType, Identifier identifier
* @param previousVersion The previous version assigned to the instance being saved.
* @param the type of the instance to save.
* @return whether the update actually updated a row.
- * @throws OptimisticLockingFailureException if the update fails to update at least one row assuming the the optimistic locking version check failed.
+ * @throws OptimisticLockingFailureException if the update fails to update at least one row assuming the the
+ * optimistic locking version check failed.
* @since 2.0
*/
boolean updateWithVersion(T instance, Class domainType, Number previousVersion);
@@ -113,7 +116,8 @@ default Object insert(T instance, Class domainType, Identifier identifier
* @param domainType the type of entity to be deleted. Implicitly determines the table to operate on. Must not be
* {@code null}.
* @param previousVersion The previous version assigned to the instance being saved.
- * @throws OptimisticLockingFailureException if the update fails to update at least one row assuming the the optimistic locking version check failed.
+ * @throws OptimisticLockingFailureException if the update fails to update at least one row assuming the the
+ * optimistic locking version check failed.
* @since 2.0
*/
void deleteWithVersion(Object id, Class domainType, Number previousVersion);
diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java
index 298fd6ac6f..12179bd1f1 100644
--- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java
+++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java
@@ -15,13 +15,11 @@
*/
package org.springframework.data.jdbc.core.convert;
-import static org.springframework.data.jdbc.core.convert.SqlGenerator.VERSION_SQL_PARAMETER_NAME;
+import static org.springframework.data.jdbc.core.convert.SqlGenerator.*;
import java.sql.JDBCType;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -36,15 +34,16 @@
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyHandler;
-import org.springframework.data.relational.core.conversion.RelationalEntityVersionUtils;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.domain.Identifier;
+import org.springframework.data.relational.domain.IdentifierProcessing;
+import org.springframework.data.relational.domain.SqlIdentifier;
import org.springframework.jdbc.core.RowMapper;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
+import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.KeyHolder;
@@ -98,7 +97,8 @@ public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, Relation
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
*/
@Override
- public Object insert(T instance, Class domainType, Map additionalParameters) {
+ @Nullable
+ public Object insert(T instance, Class domainType, Map additionalParameters) {
return insert(instance, domainType, Identifier.from(additionalParameters));
}
@@ -109,11 +109,11 @@ public Object insert(T instance, Class domainType, Map ad
@Override
public Object insert(T instance, Class domainType, Identifier identifier) {
- KeyHolder holder = new GeneratedKeyHolder();
+ SqlGenerator sqlGenerator = sql(domainType);
RelationalPersistentEntity persistentEntity = getRequiredPersistentEntity(domainType);
- MapSqlParameterSource parameterSource = getParameterSource(instance, persistentEntity, "",
- PersistentProperty::isIdProperty);
+ SqlIdentifierParameterSource parameterSource = getParameterSource(instance, persistentEntity, "",
+ PersistentProperty::isIdProperty, getIdentifierProcessing());
identifier.forEach((name, value, type) -> addConvertedPropertyValue(parameterSource, name, value, type));
@@ -124,8 +124,10 @@ public Object insert(T instance, Class domainType, Identifier identifier)
addConvertedPropertyValue(parameterSource, idProperty, idValue, idProperty.getColumnName());
}
+ KeyHolder holder = new GeneratedKeyHolder();
+
operations.update( //
- sql(domainType).getInsert(new HashSet<>(Arrays.asList(parameterSource.getParameterNames()))), //
+ sqlGenerator.getInsert(new HashSet<>(parameterSource.getIdentifiers())), //
parameterSource, //
holder //
);
@@ -142,7 +144,7 @@ public boolean update(S instance, Class domainType) {
RelationalPersistentEntity persistentEntity = getRequiredPersistentEntity(domainType);
return operations.update(sql(domainType).getUpdate(),
- getParameterSource(instance, persistentEntity, "", Predicates.includeAll())) != 0;
+ getParameterSource(instance, persistentEntity, "", Predicates.includeAll(), getIdentifierProcessing())) != 0;
}
/*
@@ -155,13 +157,14 @@ public boolean updateWithVersion(S instance, Class domainType, Number pre
RelationalPersistentEntity persistentEntity = getRequiredPersistentEntity(domainType);
// Adjust update statement to set the new version and use the old version in where clause.
- MapSqlParameterSource parameterSource = getParameterSource(instance, persistentEntity, "",
- Predicates.includeAll());
- parameterSource.addValue(VERSION_SQL_PARAMETER_NAME, previousVersion);
+ SqlIdentifierParameterSource parameterSource = getParameterSource(instance, persistentEntity, "",
+ Predicates.includeAll(), getIdentifierProcessing());
+ parameterSource.addValue(VERSION_SQL_PARAMETER, previousVersion);
int affectedRows = operations.update(sql(domainType).getUpdateWithVersion(), parameterSource);
if (affectedRows == 0) {
+
throw new OptimisticLockingFailureException(
String.format("Optimistic lock exception on saving entity of type %s.", persistentEntity.getName()));
}
@@ -177,7 +180,7 @@ public boolean updateWithVersion(S instance, Class domainType, Number pre
public void delete(Object id, Class> domainType) {
String deleteByIdSql = sql(domainType).getDeleteById();
- MapSqlParameterSource parameter = createIdParameterSource(id, domainType);
+ SqlParameterSource parameter = createIdParameterSource(id, domainType);
operations.update(deleteByIdSql, parameter);
}
@@ -193,8 +196,8 @@ public void deleteWithVersion(Object id, Class domainType, Number previou
RelationalPersistentEntity persistentEntity = getRequiredPersistentEntity(domainType);
- MapSqlParameterSource parameterSource = createIdParameterSource(id, domainType);
- parameterSource.addValue(VERSION_SQL_PARAMETER_NAME, previousVersion);
+ SqlIdentifierParameterSource parameterSource = createIdParameterSource(id, domainType);
+ parameterSource.addValue(VERSION_SQL_PARAMETER, previousVersion);
int affectedRows = operations.update(sql(domainType).getDeleteByIdAndVersion(), parameterSource);
if (affectedRows == 0) {
@@ -216,11 +219,11 @@ public void delete(Object rootId, PersistentPropertyPath parameters = new HashMap<>();
- parameters.put("rootId", rootId);
- operations.update(format, parameters);
+ SqlIdentifierParameterSource parameters = new SqlIdentifierParameterSource(getIdentifierProcessing());
+ parameters.addValue(ROOT_ID_PARAMETER, rootId);
+ operations.update(delete, parameters);
}
/*
@@ -265,7 +268,7 @@ public long count(Class> domainType) {
public T findById(Object id, Class domainType) {
String findOneSql = sql(domainType).getFindOne();
- MapSqlParameterSource parameter = createIdParameterSource(id, domainType);
+ SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
try {
return operations.queryForObject(findOneSql, parameter, (RowMapper) getEntityRowMapper(domainType));
@@ -297,9 +300,9 @@ public Iterable findAllById(Iterable> ids, Class domainType) {
}
RelationalPersistentProperty idProperty = getRequiredPersistentEntity(domainType).getRequiredIdProperty();
- MapSqlParameterSource parameterSource = new MapSqlParameterSource();
+ SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(getIdentifierProcessing());
- addConvertedPropertyValuesAsList(parameterSource, idProperty, ids, "ids");
+ addConvertedPropertyValuesAsList(parameterSource, idProperty, ids, IDS_SQL_PARAMETER);
String findAllInListSql = sql(domainType).getFindAllInList();
@@ -324,12 +327,20 @@ public Iterable