Skip to content

Commit af1a414

Browse files
committed
DATAJDBC-219: replaced VersionAccessor with simpler code
added integration tests for all version types
1 parent 080d8ae commit af1a414

File tree

4 files changed

+168
-310
lines changed

4 files changed

+168
-310
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Collectors;
2727
import java.util.stream.StreamSupport;
2828

29+
import org.springframework.core.convert.ConversionService;
2930
import org.springframework.dao.DataRetrievalFailureException;
3031
import org.springframework.dao.EmptyResultDataAccessException;
3132
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -34,6 +35,7 @@
3435
import org.springframework.data.mapping.PersistentPropertyAccessor;
3536
import org.springframework.data.mapping.PersistentPropertyPath;
3637
import org.springframework.data.mapping.PropertyHandler;
38+
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
3739
import org.springframework.data.relational.core.conversion.RelationalConverter;
3840
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3941
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@@ -71,7 +73,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
7173
* Only suitable if this is the only access strategy in use.
7274
*/
7375
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
74-
RelationalConverter converter, NamedParameterJdbcOperations operations) {
76+
RelationalConverter converter, NamedParameterJdbcOperations operations) {
7577

7678
this.sqlGeneratorSource = sqlGeneratorSource;
7779
this.operations = operations;
@@ -117,9 +119,9 @@ public <T> Object insert(T instance, Class<T> domainType, Identifier identifier)
117119
}
118120

119121
if (persistentEntity.hasVersionProperty()) {
120-
VersionAccessor versionAccessor = new VersionAccessor<>(instance, persistentEntity);
121-
Number newVersion = versionAccessor.nextVersion();
122-
versionAccessor.setVersion(newVersion);
122+
123+
Number newVersion = getNextVersion(instance, persistentEntity, converter.getConversionService());
124+
setVersion(instance, persistentEntity, newVersion);
123125
parameters.put(persistentEntity.getVersionProperty().getColumnName(), converter.writeValue(newVersion,
124126
ClassTypeInformation.from(persistentEntity.getVersionProperty().getColumnType())));
125127
}
@@ -159,18 +161,17 @@ private <S> boolean updateWithoutVersion(S instance, Class<S> domainType) {
159161
private <S> boolean updateWithVersion(S instance, Class<S> domainType) {
160162

161163
RelationalPersistentEntity<S> persistentEntity = getRequiredPersistentEntity(domainType);
162-
VersionAccessor<S> versionAccessor = new VersionAccessor<>(instance, persistentEntity);
163164

164-
Number oldVersion = versionAccessor.currentVersion();
165-
Number newVersion = versionAccessor.nextVersion();
166-
versionAccessor.setVersion(newVersion);
165+
Number oldVersion = getVersion(instance, persistentEntity, converter.getConversionService());
166+
Number newVersion = getNextVersion(instance, persistentEntity, converter.getConversionService());
167+
setVersion(instance, persistentEntity, newVersion);
167168

168169
int affectedRows = operations.update(sql(domainType).getUpdateWithVersion(oldVersion),
169170
getPropertyMap(instance, persistentEntity, ""));
170171

171172
if (affectedRows == 0) {
172173
// reverting version update on entity
173-
versionAccessor.setVersion(oldVersion);
174+
setVersion(instance, persistentEntity, oldVersion);
174175
throw new OptimisticLockingFailureException(
175176
String.format("Optimistic lock exception on saving entity of type %s.", persistentEntity.getName()));
176177
}
@@ -333,7 +334,7 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
333334
}
334335

335336
private <S, T> MapSqlParameterSource getPropertyMap(S instance, RelationalPersistentEntity<S> persistentEntity,
336-
String prefix) {
337+
String prefix) {
337338

338339
MapSqlParameterSource parameters = new MapSqlParameterSource();
339340

@@ -396,7 +397,7 @@ private <S, ID> ID getIdValueOrNull(S instance, RelationalPersistentEntity<S> pe
396397
}
397398

398399
private static <S, ID> boolean isIdPropertyNullOrScalarZero(@Nullable ID idValue,
399-
RelationalPersistentEntity<S> persistentEntity) {
400+
RelationalPersistentEntity<S> persistentEntity) {
400401

401402
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
402403
return idValue == null //
@@ -452,4 +453,31 @@ private SqlGenerator sql(Class<?> domainType) {
452453
return sqlGeneratorSource.getSqlGenerator(domainType);
453454
}
454455

456+
@Nullable
457+
private <T> Number getVersion(T instance, RelationalPersistentEntity<T> entity, ConversionService conversionService) {
458+
RelationalPersistentProperty versionProperty = entity.getRequiredVersionProperty();
459+
PersistentPropertyAccessor<T> propertyAccessor = entity.getPropertyAccessor(instance);
460+
ConvertingPropertyAccessor<T> convertingPropertyAccessor = new ConvertingPropertyAccessor<>(propertyAccessor, conversionService);
461+
return convertingPropertyAccessor.getProperty(versionProperty, Number.class);
462+
}
463+
464+
private <T> Number getNextVersion(T instance, RelationalPersistentEntity<T> entity, ConversionService conversionService) {
465+
Number version = getVersion(instance, entity, conversionService);
466+
Class<?> versionType = entity.getRequiredVersionProperty().getType();
467+
if (versionType == Integer.class || versionType == int.class) {
468+
return version == null ? 1 : version.intValue() + 1;
469+
} else if (versionType == Long.class || versionType == long.class) {
470+
return version == null ? 1L : version.longValue() + 1;
471+
} else if (versionType == Short.class || versionType == short.class) {
472+
return version == null ? (short) 1 : (short) (version.shortValue() + 1);
473+
}
474+
throw new IllegalStateException(String.format("Entity '%s' has version property of invalid type '%s'.", entity.getType().getName(), entity.getVersionProperty().getType().getName()));
475+
}
476+
477+
private <T> void setVersion(T instance, RelationalPersistentEntity<T> entity, Number newVersion) {
478+
RelationalPersistentProperty versionProperty = entity.getRequiredVersionProperty();
479+
PersistentPropertyAccessor<T> accessor = versionProperty.getOwner().getPropertyAccessor(instance);
480+
accessor.setProperty(versionProperty, newVersion);
481+
}
482+
455483
}

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

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

0 commit comments

Comments
 (0)