Skip to content

Commit 323a56c

Browse files
schauderodrotbohm
authored andcommitted
DATAJDBC-115 - Adapt to changes in Spring Commons API.
Original pull request: #8.
1 parent b05886e commit 323a56c

File tree

6 files changed

+31
-30
lines changed

6 files changed

+31
-30
lines changed

src/main/java/org/springframework/data/jdbc/repository/EntityRowMapper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import org.springframework.data.convert.EntityInstantiator;
2828
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
2929
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
30+
import org.springframework.data.mapping.MappingException;
3031
import org.springframework.data.mapping.PersistentProperty;
3132
import org.springframework.data.mapping.PersistentPropertyAccessor;
3233
import org.springframework.data.mapping.PreferredConstructor.Parameter;
3334
import org.springframework.data.mapping.PropertyHandler;
3435
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
35-
import org.springframework.data.mapping.model.MappingException;
3636
import org.springframework.data.mapping.model.ParameterValueProvider;
3737
import org.springframework.jdbc.core.RowMapper;
3838

@@ -74,10 +74,10 @@ private T createInstance(ResultSet rs) {
7474
return instantiator.createInstance(entity, ResultSetParameterValueProvider.of(rs, conversions));
7575
}
7676

77-
private static Optional<Object> readFrom(ResultSet resultSet, PersistentProperty<?> property) {
77+
private static Object readFrom(ResultSet resultSet, PersistentProperty<?> property) {
7878

7979
try {
80-
return Optional.ofNullable(resultSet.getObject(property.getName()));
80+
return resultSet.getObject(property.getName());
8181
} catch (SQLException o_O) {
8282
throw new MappingException(String.format("Could not read property %s from result set!", property), o_O);
8383
}
@@ -94,16 +94,16 @@ private static class ResultSetParameterValueProvider implements ParameterValuePr
9494
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
9595
*/
9696
@Override
97-
public <S> Optional<S> getParameterValue(Parameter<S, JdbcPersistentProperty> parameter) {
97+
public <T> T getParameterValue(Parameter<T, JdbcPersistentProperty> parameter) {
9898

99-
return parameter.getName().map(name -> {
99+
String name = parameter.getName();
100+
if (name == null ) return null;
100101

101102
try {
102103
return conversionService.convert(resultSet.getObject(name), parameter.getType().getType());
103104
} catch (SQLException o_O) {
104105
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", name), o_O);
105106
}
106-
});
107107
}
108108
}
109109
}

src/main/java/org/springframework/data/jdbc/repository/SimpleJdbcRepository.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ private <S extends T> Map<String, Object> getPropertyMap(final S instance) {
210210

211211
this.persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
212212

213-
Optional<Object> value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
214-
parameters.put(property.getColumnName(), value.orElse(null));
213+
Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
214+
parameters.put(property.getColumnName(), value);
215215
});
216216

217217
return parameters;
@@ -238,17 +238,17 @@ private <S extends T> void doInsert(S instance) {
238238

239239
private <S extends T> ID getIdValueOrNull(S instance) {
240240

241-
Optional<ID> idValue = entityInformation.getId(instance);
242-
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue.get();
241+
ID idValue = entityInformation.getId(instance);
242+
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue;
243243
}
244244

245-
private boolean isIdPropertySimpleTypeAndValueZero(Optional<ID> idValue) {
245+
private boolean isIdPropertySimpleTypeAndValueZero(ID idValue) {
246246

247-
Optional<JdbcPersistentProperty> idProperty = persistentEntity.getIdProperty();
248-
return !idValue.isPresent() //
249-
|| !idProperty.isPresent() //
250-
|| (((Optional<JdbcPersistentProperty>) idProperty).get().getType() == int.class && idValue.equals(0)) //
251-
|| (((Optional<JdbcPersistentProperty>) idProperty).get().getType() == long.class && idValue.equals(0L));
247+
JdbcPersistentProperty idProperty = persistentEntity.getIdProperty();
248+
return idValue == null //
249+
|| idProperty == null //
250+
|| (idProperty.getType() == int.class && idValue.equals(0)) //
251+
|| (idProperty.getType() == long.class && idValue.equals(0L));
252252
}
253253

254254
private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) {
@@ -259,7 +259,7 @@ private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) {
259259

260260
Class<?> targetType = persistentEntity.getRequiredIdProperty().getType();
261261
Object converted = convert(it, targetType);
262-
entityInformation.setId(instance, Optional.of(converted));
262+
entityInformation.setId(instance, converted);
263263
});
264264

265265
} catch (NonTransientDataAccessException e) {

src/main/java/org/springframework/data/jdbc/repository/support/BasicJdbcPersistentEntityInformation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public BasicJdbcPersistentEntityInformation(JdbcPersistentEntity<T> persistentEn
4242
* @see org.springframework.data.jdbc.repository.support.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional)
4343
*/
4444
@Override
45-
public void setId(T instance, Optional<Object> value) {
45+
public void setId(T instance, Object value) {
4646
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getRequiredIdProperty(), value);
4747
}
4848
}

src/main/java/org/springframework/data/jdbc/repository/support/JdbcPersistentEntityInformation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.support;
1717

18-
import java.io.Serializable;
1918
import java.util.Optional;
2019

2120
import org.springframework.data.repository.core.EntityInformation;
@@ -26,7 +25,7 @@
2625
*/
2726
public interface JdbcPersistentEntityInformation<T, ID> extends EntityInformation<T, ID> {
2827

29-
void setId(T instance, Optional<Object> value);
28+
void setId(T instance, Object value);
3029

3130
/**
3231
* Returns the identifier of the given entity or throws and exception if it can't be obtained.
@@ -36,7 +35,8 @@ public interface JdbcPersistentEntityInformation<T, ID> extends EntityInformatio
3635
* @throws IllegalArgumentException in case no identifier can be obtained for the given entity.
3736
*/
3837
default ID getRequiredId(T entity) {
39-
return getId(entity).orElseThrow(() -> new IllegalArgumentException(
40-
String.format("Could not obtain required identifier from entity %s!", entity)));
38+
ID id = getId(entity);
39+
if (id == null) throw new IllegalStateException(String.format("Could not obtain required identifier from entity %s!", entity));
40+
return id;
4141
}
4242
}

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
import lombok.RequiredArgsConstructor;
1919

20-
import java.io.Serializable;
21-
2220
import org.springframework.context.ApplicationEventPublisher;
2321
import org.springframework.data.jdbc.mapping.context.JdbcMappingContext;
2422
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
23+
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityImpl;
2524
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
2625
import org.springframework.data.repository.core.EntityInformation;
2726
import org.springframework.data.repository.core.RepositoryInformation;
@@ -44,16 +43,18 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
4443
@Override
4544
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
4645

47-
return context.getPersistentEntity(aClass)
48-
.map(e -> new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) e)).orElseGet(null);
46+
JdbcPersistentEntityImpl<?> persistentEntity = context.getPersistentEntity(aClass);
47+
if (persistentEntity == null)
48+
return null;
49+
return new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) persistentEntity);
4950
}
5051

5152
@Override
5253
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
5354

54-
JdbcPersistentEntity<?> persistentEntity = context //
55-
.getPersistentEntity(repositoryInformation.getDomainType()) //
56-
.orElseThrow(() -> new IllegalArgumentException("%s does not represent a persistent entity")); //
55+
JdbcPersistentEntity<?> persistentEntity = context
56+
.getRequiredPersistentEntity(repositoryInformation.getDomainType());
57+
5758
return new SimpleJdbcRepository<>(persistentEntity, jdbcOperations, publisher);
5859
}
5960

src/test/java/org/springframework/data/jdbc/repository/EventPublishingEntityRowMapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class EventPublishingEntityRowMapperTest {
5151
public void eventGetsPublishedAfterInstantiation() throws SQLException {
5252

5353
when(rowMapperDelegate.mapRow(any(ResultSet.class), anyInt())).thenReturn(new DummyEntity(1L));
54-
when(entityInformation.getId(any())).thenReturn(Optional.of(1L));
54+
when(entityInformation.getId(any())).thenReturn(1L);
5555

5656
EventPublishingEntityRowMapper<?> rowMapper = new EventPublishingEntityRowMapper<>(rowMapperDelegate,
5757
entityInformation, publisher);

0 commit comments

Comments
 (0)