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;
import org.springframework.data.convert.EntityInstantiator; import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty; 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.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor.Parameter; import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor; import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.ParameterValueProvider; import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
@ -74,10 +74,10 @@ class EntityRowMapper<T> implements RowMapper<T> {
return instantiator.createInstance(entity, ResultSetParameterValueProvider.of(rs, conversions)); 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 { try {
return Optional.ofNullable(resultSet.getObject(property.getName())); return resultSet.getObject(property.getName());
} catch (SQLException o_O) { } catch (SQLException o_O) {
throw new MappingException(String.format("Could not read property %s from result set!", property), 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> {
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter) * @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/ */
@Override @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 { try {
return conversionService.convert(resultSet.getObject(name), parameter.getType().getType()); return conversionService.convert(resultSet.getObject(name), parameter.getType().getType());
} catch (SQLException o_O) { } catch (SQLException o_O) {
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", name), 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
this.persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> { this.persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
Optional<Object> value = persistentEntity.getPropertyAccessor(instance).getProperty(property); Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
parameters.put(property.getColumnName(), value.orElse(null)); parameters.put(property.getColumnName(), value);
}); });
return parameters; return parameters;
@ -238,17 +238,17 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
private <S extends T> ID getIdValueOrNull(S instance) { private <S extends T> ID getIdValueOrNull(S instance) {
Optional<ID> idValue = entityInformation.getId(instance); ID idValue = entityInformation.getId(instance);
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue.get(); return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue;
} }
private boolean isIdPropertySimpleTypeAndValueZero(Optional<ID> idValue) { private boolean isIdPropertySimpleTypeAndValueZero(ID idValue) {
Optional<JdbcPersistentProperty> idProperty = persistentEntity.getIdProperty(); JdbcPersistentProperty idProperty = persistentEntity.getIdProperty();
return !idValue.isPresent() // return idValue == null //
|| !idProperty.isPresent() // || idProperty == null //
|| (((Optional<JdbcPersistentProperty>) idProperty).get().getType() == int.class && idValue.equals(0)) // || (idProperty.getType() == int.class && idValue.equals(0)) //
|| (((Optional<JdbcPersistentProperty>) idProperty).get().getType() == long.class && idValue.equals(0L)); || (idProperty.getType() == long.class && idValue.equals(0L));
} }
private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) { private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) {
@ -259,7 +259,7 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
Class<?> targetType = persistentEntity.getRequiredIdProperty().getType(); Class<?> targetType = persistentEntity.getRequiredIdProperty().getType();
Object converted = convert(it, targetType); Object converted = convert(it, targetType);
entityInformation.setId(instance, Optional.of(converted)); entityInformation.setId(instance, converted);
}); });
} catch (NonTransientDataAccessException e) { } 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
* @see org.springframework.data.jdbc.repository.support.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional) * @see org.springframework.data.jdbc.repository.support.JdbcPersistentEntityInformation#setId(java.lang.Object, java.util.Optional)
*/ */
@Override @Override
public void setId(T instance, Optional<Object> value) { public void setId(T instance, Object value) {
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getRequiredIdProperty(), value); persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getRequiredIdProperty(), value);
} }
} }

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

@ -15,7 +15,6 @@
*/ */
package org.springframework.data.jdbc.repository.support; package org.springframework.data.jdbc.repository.support;
import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.repository.core.EntityInformation; 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> { 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. * 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
* @throws IllegalArgumentException in case no identifier can be obtained for the given entity. * @throws IllegalArgumentException in case no identifier can be obtained for the given entity.
*/ */
default ID getRequiredId(T entity) { default ID getRequiredId(T entity) {
return getId(entity).orElseThrow(() -> new IllegalArgumentException( ID id = getId(entity);
String.format("Could not obtain required identifier from entity %s!", 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;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.mapping.context.JdbcMappingContext; import org.springframework.data.jdbc.mapping.context.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; 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.jdbc.repository.SimpleJdbcRepository;
import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.core.RepositoryInformation;
@ -44,16 +43,18 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
@Override @Override
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) { public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
return context.getPersistentEntity(aClass) JdbcPersistentEntityImpl<?> persistentEntity = context.getPersistentEntity(aClass);
.map(e -> new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) e)).orElseGet(null); if (persistentEntity == null)
return null;
return new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) persistentEntity);
} }
@Override @Override
protected Object getTargetRepository(RepositoryInformation repositoryInformation) { protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
JdbcPersistentEntity<?> persistentEntity = context // JdbcPersistentEntity<?> persistentEntity = context
.getPersistentEntity(repositoryInformation.getDomainType()) // .getRequiredPersistentEntity(repositoryInformation.getDomainType());
.orElseThrow(() -> new IllegalArgumentException("%s does not represent a persistent entity")); //
return new SimpleJdbcRepository<>(persistentEntity, jdbcOperations, publisher); return new SimpleJdbcRepository<>(persistentEntity, jdbcOperations, publisher);
} }

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

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

Loading…
Cancel
Save