DATAJDBC-109 - Adapt to API changes in Spring Data Commons.
Original pull request: #5.
This commit is contained in:
committed by
Oliver Gierke
parent
1582a4d475
commit
f0eaf862cf
@@ -15,13 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.context;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentProperty;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext;
|
||||
import org.springframework.data.mapping.model.Property;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
@@ -38,11 +36,11 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
|
||||
|
||||
@Override
|
||||
protected JdbcPersistentProperty createPersistentProperty( //
|
||||
Field field, //
|
||||
PropertyDescriptor descriptor, //
|
||||
JdbcPersistentEntity owner, //
|
||||
Property property, //
|
||||
JdbcPersistentEntity<?> owner, //
|
||||
SimpleTypeHolder simpleTypeHolder //
|
||||
) {
|
||||
return new BasicJdbcPersistentProperty(field, descriptor, owner, simpleTypeHolder);
|
||||
return new BasicJdbcPersistentProperty(property, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,12 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.Property;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
/**
|
||||
@@ -35,18 +33,16 @@ public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProper
|
||||
/**
|
||||
* Creates a new {@link AnnotationBasedPersistentProperty}.
|
||||
*
|
||||
* @param field must not be {@literal null}.
|
||||
* @param propertyDescriptor can be {@literal null}.
|
||||
* @param property must not be {@literal null}.
|
||||
* @param owner must not be {@literal null}.
|
||||
* @param simpleTypeHolder
|
||||
*/
|
||||
public BasicJdbcPersistentProperty( //
|
||||
Field field, //
|
||||
PropertyDescriptor propertyDescriptor, //
|
||||
Property property, //
|
||||
PersistentEntity<?, JdbcPersistentProperty> owner, //
|
||||
SimpleTypeHolder simpleTypeHolder //
|
||||
) {
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
super(property, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -40,6 +40,6 @@ public class JdbcPersistentEntity<T> extends BasicPersistentEntity<T, JdbcPersis
|
||||
}
|
||||
|
||||
public String getIdColumn() {
|
||||
return getIdProperty().getName();
|
||||
return getRequiredIdProperty().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.jdbc.repository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
@@ -25,7 +26,7 @@ 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.PersistentProperty;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
@@ -61,21 +62,7 @@ class EntityRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
private T createInstance(ResultSet rs) {
|
||||
|
||||
return instantiator.createInstance(entity, new ParameterValueProvider<JdbcPersistentProperty>() {
|
||||
|
||||
@Override
|
||||
public <T> T getParameterValue(PreferredConstructor.Parameter<T, JdbcPersistentProperty> parameter) {
|
||||
|
||||
try {
|
||||
return conversions.convert(rs.getObject(parameter.getName()), parameter.getType().getType());
|
||||
} catch (SQLException e) {
|
||||
|
||||
throw new MappingException( //
|
||||
String.format("Couldn't read column %s from ResultSet.", parameter.getName()) //
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
return instantiator.createInstance(entity, new ResultSetParameterValueProvider(rs));
|
||||
}
|
||||
|
||||
private void setProperty(ResultSet rs, T t, PersistentProperty property) {
|
||||
@@ -83,9 +70,33 @@ class EntityRowMapper<T> implements RowMapper<T> {
|
||||
try {
|
||||
|
||||
Object converted = conversions.convert(rs.getObject(property.getName()), property.getType());
|
||||
entity.getPropertyAccessor(t).setProperty(property, converted);
|
||||
entity.getPropertyAccessor(t).setProperty(property, Optional.of(converted));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(String.format("Couldn't set property %s.", property.getName()), e);
|
||||
}
|
||||
}
|
||||
|
||||
private class ResultSetParameterValueProvider implements ParameterValueProvider<JdbcPersistentProperty> {
|
||||
|
||||
private final ResultSet rs;
|
||||
|
||||
ResultSetParameterValueProvider(ResultSet rs) {
|
||||
this.rs = rs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> Optional<S> getParameterValue(Parameter<S, JdbcPersistentProperty> parameter) {
|
||||
|
||||
return parameter.getName().map(name -> {
|
||||
try {
|
||||
return conversions.convert(rs.getObject(name), parameter.getType().getType());
|
||||
} catch (SQLException e) {
|
||||
|
||||
throw new MappingException( //
|
||||
String.format("Couldn't read column %s from ResultSet.", name) //
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,9 +106,10 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(ID id) {
|
||||
public Optional<T> findOne(ID id) {
|
||||
|
||||
return operations.queryForObject(sql.getFindOne(), new MapSqlParameterSource("id", id), entityRowMapper);
|
||||
return Optional
|
||||
.ofNullable(operations.queryForObject(sql.getFindOne(), new MapSqlParameterSource("id", id), entityRowMapper));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -139,14 +140,15 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
|
||||
|
||||
@Override
|
||||
public void delete(T instance) {
|
||||
doDelete(new Specified(entityInformation.getId(instance)), Optional.of(instance));
|
||||
doDelete(new Specified(entityInformation.getId(instance).orElse(null)), Optional.of(instance));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
List<ID> idList = StreamSupport.stream(entities.spliterator(), false) //
|
||||
.map(entityInformation::getId) //
|
||||
.map(e -> entityInformation.getId(e)
|
||||
.orElseThrow(() -> new IllegalArgumentException(String.format("Can't obtain id for %s", e)))) //
|
||||
.collect(Collectors.toList());
|
||||
|
||||
MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource("ids", idList);
|
||||
@@ -164,8 +166,8 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
|
||||
|
||||
this.persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) property -> {
|
||||
|
||||
Object value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
|
||||
parameters.put(property.getColumnName(), value);
|
||||
Optional<Object> value = persistentEntity.getPropertyAccessor(instance).getProperty(property);
|
||||
parameters.put(property.getColumnName(), value.orElse(null));
|
||||
});
|
||||
|
||||
return parameters;
|
||||
@@ -178,7 +180,7 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
|
||||
KeyHolder holder = new GeneratedKeyHolder();
|
||||
|
||||
Map<String, Object> propertyMap = getPropertyMap(instance);
|
||||
propertyMap.put(persistentEntity.getIdColumn(), getIdValueOrNull(instance));
|
||||
propertyMap.put(persistentEntity.getRequiredIdProperty().getColumnName(), getIdValueOrNull(instance));
|
||||
|
||||
operations.update(sql.getInsert(), new MapSqlParameterSource(propertyMap), holder);
|
||||
setIdFromJdbc(instance, holder);
|
||||
@@ -192,14 +194,17 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
|
||||
|
||||
private <S extends T> ID getIdValueOrNull(S instance) {
|
||||
|
||||
ID idValue = entityInformation.getId(instance);
|
||||
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue;
|
||||
Optional<ID> idValue = entityInformation.getId(instance);
|
||||
return isIdPropertySimpleTypeAndValueZero(idValue) ? null : idValue.get();
|
||||
}
|
||||
|
||||
private boolean isIdPropertySimpleTypeAndValueZero(ID idValue) {
|
||||
private boolean isIdPropertySimpleTypeAndValueZero(Optional<ID> idValue) {
|
||||
|
||||
return (persistentEntity.getIdProperty().getType() == int.class && idValue.equals(0))
|
||||
|| (persistentEntity.getIdProperty().getType() == long.class && idValue.equals(0L));
|
||||
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));
|
||||
}
|
||||
|
||||
private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) {
|
||||
@@ -209,9 +214,9 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
|
||||
Object idValueFromJdbc = getIdFromHolder(holder);
|
||||
if (idValueFromJdbc != null) {
|
||||
|
||||
Class<?> targetType = persistentEntity.getIdProperty().getType();
|
||||
Class<?> targetType = persistentEntity.getRequiredIdProperty().getType();
|
||||
Object converted = convert(idValueFromJdbc, targetType);
|
||||
entityInformation.setId(instance, converted);
|
||||
entityInformation.setId(instance, Optional.of(converted));
|
||||
}
|
||||
|
||||
} catch (NonTransientDataAccessException e) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.jdbc.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
@@ -36,7 +37,7 @@ public class BasicJdbcPersistentEntityInformation<T, ID extends Serializable> ex
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(T instance, Object value) {
|
||||
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getIdProperty(), value);
|
||||
public void setId(T instance, Optional<Object> value) {
|
||||
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getRequiredIdProperty(), value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.jdbc.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
@@ -25,5 +26,5 @@ import org.springframework.data.repository.core.EntityInformation;
|
||||
*/
|
||||
public interface JdbcPersistentEntityInformation<T, ID extends Serializable> extends EntityInformation<T, ID> {
|
||||
|
||||
void setId(T instance, Object value);
|
||||
void setId(T instance, Optional<Object> value);
|
||||
}
|
||||
|
||||
@@ -43,13 +43,17 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
|
||||
return new BasicJdbcPersistentEntityInformation<>((JdbcPersistentEntity<T>) context.getPersistentEntity(aClass));
|
||||
|
||||
return context.getPersistentEntity(aClass)
|
||||
.map(e -> new BasicJdbcPersistentEntityInformation<T, ID>((JdbcPersistentEntity<T>) e)).orElseGet(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
|
||||
|
||||
JdbcPersistentEntity<?> persistentEntity = context.getPersistentEntity(repositoryInformation.getDomainType());
|
||||
JdbcPersistentEntity<?> persistentEntity = context //
|
||||
.getPersistentEntity(repositoryInformation.getDomainType()) //
|
||||
.orElseThrow(() -> new IllegalArgumentException("%s does not represent a persistent entity")); //
|
||||
return new SimpleJdbcRepository<>(persistentEntity, jdbcOperations, publisher);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user