Browse Source

DATAJDBC-115 - Adapt to changes in Spring Commons API.

Original pull request: #8.
pull/9/head
Jens Schauder 9 years ago committed by Oliver Gierke
parent
commit
323a56c156
  1. 12
      src/main/java/org/springframework/data/jdbc/repository/EntityRowMapper.java
  2. 22
      src/main/java/org/springframework/data/jdbc/repository/SimpleJdbcRepository.java
  3. 2
      src/main/java/org/springframework/data/jdbc/repository/support/BasicJdbcPersistentEntityInformation.java
  4. 8
      src/main/java/org/springframework/data/jdbc/repository/support/JdbcPersistentEntityInformation.java
  5. 15
      src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java
  6. 2
      src/test/java/org/springframework/data/jdbc/repository/EventPublishingEntityRowMapperTest.java

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

@ -27,12 +27,12 @@ import org.springframework.data.convert.ClassGeneratingEntityInstantiator; @@ -27,12 +27,12 @@ import org.springframework.data.convert.ClassGeneratingEntityInstantiator;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.jdbc.core.RowMapper;
@ -74,10 +74,10 @@ class EntityRowMapper<T> implements RowMapper<T> { @@ -74,10 +74,10 @@ class EntityRowMapper<T> implements RowMapper<T> {
return instantiator.createInstance(entity, ResultSetParameterValueProvider.of(rs, conversions));
}
private static Optional<Object> readFrom(ResultSet resultSet, PersistentProperty<?> property) {
private static Object readFrom(ResultSet resultSet, PersistentProperty<?> property) {
try {
return Optional.ofNullable(resultSet.getObject(property.getName()));
return resultSet.getObject(property.getName());
} catch (SQLException o_O) {
throw new MappingException(String.format("Could not read property %s from result set!", property), o_O);
}
@ -94,16 +94,16 @@ class EntityRowMapper<T> implements RowMapper<T> { @@ -94,16 +94,16 @@ class EntityRowMapper<T> implements RowMapper<T> {
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/
@Override
public <S> Optional<S> getParameterValue(Parameter<S, JdbcPersistentProperty> parameter) {
public <T> T getParameterValue(Parameter<T, JdbcPersistentProperty> parameter) {
return parameter.getName().map(name -> {
String name = parameter.getName();
if (name == null ) return null;
try {
return conversionService.convert(resultSet.getObject(name), parameter.getType().getType());
} catch (SQLException o_O) {
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", name), o_O);
}
});
}
}
}

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

@ -210,8 +210,8 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep @@ -210,8 +210,8 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
this.persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
Optional<Object> value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
parameters.put(property.getColumnName(), value.orElse(null));
Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
parameters.put(property.getColumnName(), value);
});
return parameters;
@ -238,17 +238,17 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep @@ -238,17 +238,17 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
private <S extends T> ID getIdValueOrNull(S instance) {
Optional<ID> idValue = entityInformation.getId(instance);
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue.get();
ID idValue = entityInformation.getId(instance);
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue;
}
private boolean isIdPropertySimpleTypeAndValueZero(Optional<ID> idValue) {
private boolean isIdPropertySimpleTypeAndValueZero(ID idValue) {
Optional<JdbcPersistentProperty> idProperty = persistentEntity.getIdProperty();
return !idValue.isPresent() //
|| !idProperty.isPresent() //
|| (((Optional<JdbcPersistentProperty>) idProperty).get().getType() == int.class && idValue.equals(0)) //
|| (((Optional<JdbcPersistentProperty>) idProperty).get().getType() == long.class && idValue.equals(0L));
JdbcPersistentProperty idProperty = persistentEntity.getIdProperty();
return idValue == null //
|| idProperty == null //
|| (idProperty.getType() == int.class && idValue.equals(0)) //
|| (idProperty.getType() == long.class && idValue.equals(0L));
}
private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) {
@ -259,7 +259,7 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep @@ -259,7 +259,7 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
Class<?> targetType = persistentEntity.getRequiredIdProperty().getType();
Object converted = convert(it, targetType);
entityInformation.setId(instance, Optional.of(converted));
entityInformation.setId(instance, converted);
});
} catch (NonTransientDataAccessException e) {

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

@ -42,7 +42,7 @@ public class BasicJdbcPersistentEntityInformation<T, ID> extends PersistentEntit @@ -42,7 +42,7 @@ public class BasicJdbcPersistentEntityInformation<T, ID> extends PersistentEntit
* @see org.springframework.data.jdbc.repository.support.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional)
*/
@Override
public void setId(T instance, Optional<Object> value) {
public void setId(T instance, Object value) {
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getRequiredIdProperty(), value);
}
}

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

@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
*/
package org.springframework.data.jdbc.repository.support;
import java.io.Serializable;
import java.util.Optional;
import org.springframework.data.repository.core.EntityInformation;
@ -26,7 +25,7 @@ import org.springframework.data.repository.core.EntityInformation; @@ -26,7 +25,7 @@ import org.springframework.data.repository.core.EntityInformation;
*/
public interface JdbcPersistentEntityInformation<T, ID> extends EntityInformation<T, ID> {
void setId(T instance, Optional<Object> value);
void setId(T instance, Object value);
/**
* 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 @@ -36,7 +35,8 @@ public interface JdbcPersistentEntityInformation<T, ID> extends EntityInformatio
* @throws IllegalArgumentException in case no identifier can be obtained for the given entity.
*/
default ID getRequiredId(T entity) {
return getId(entity).orElseThrow(() -> new IllegalArgumentException(
String.format("Could not obtain required identifier from entity %s!", entity)));
ID id = getId(entity);
if (id == null) throw new IllegalStateException(String.format("Could not obtain required identifier from entity %s!", entity));
return id;
}
}

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

@ -17,11 +17,10 @@ package org.springframework.data.jdbc.repository.support; @@ -17,11 +17,10 @@ package org.springframework.data.jdbc.repository.support;
import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.mapping.context.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityImpl;
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
@ -44,16 +43,18 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport { @@ -44,16 +43,18 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
@Override
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
return context.getPersistentEntity(aClass)
.map(e -> new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) e)).orElseGet(null);
JdbcPersistentEntityImpl<?> persistentEntity = context.getPersistentEntity(aClass);
if (persistentEntity == null)
return null;
return new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) persistentEntity);
}
@Override
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
JdbcPersistentEntity<?> persistentEntity = context //
.getPersistentEntity(repositoryInformation.getDomainType()) //
.orElseThrow(() -> new IllegalArgumentException("%s does not represent a persistent entity")); //
JdbcPersistentEntity<?> persistentEntity = context
.getRequiredPersistentEntity(repositoryInformation.getDomainType());
return new SimpleJdbcRepository<>(persistentEntity, jdbcOperations, publisher);
}

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

@ -51,7 +51,7 @@ public class EventPublishingEntityRowMapperTest { @@ -51,7 +51,7 @@ public class EventPublishingEntityRowMapperTest {
public void eventGetsPublishedAfterInstantiation() throws SQLException {
when(rowMapperDelegate.mapRow(any(ResultSet.class), anyInt())).thenReturn(new DummyEntity(1L));
when(entityInformation.getId(any())).thenReturn(Optional.of(1L));
when(entityInformation.getId(any())).thenReturn(1L);
EventPublishingEntityRowMapper<?> rowMapper = new EventPublishingEntityRowMapper<>(rowMapperDelegate,
entityInformation, publisher);

Loading…
Cancel
Save