Skip to content

DATAJDBC-115 - Adapt to changes in SD Commons API. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>1.0.0.DATAJDBC-115-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
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;

Expand Down Expand Up @@ -74,10 +74,10 @@ private T createInstance(ResultSet rs) {
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);
}
Expand All @@ -94,16 +94,16 @@ private static class ResultSetParameterValueProvider implements ParameterValuePr
* @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);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ private <S extends T> Map<String, Object> getPropertyMap(final S instance) {

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;
Expand All @@ -238,17 +238,17 @@ private <S extends T> void doInsert(S instance) {

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) {
Expand All @@ -259,7 +259,7 @@ private <S extends T> void setIdFromJdbc(S instance, KeyHolder holder) {

Class<?> targetType = persistentEntity.getRequiredIdProperty().getType();
Object converted = convert(it, targetType);
entityInformation.setId(instance, Optional.of(converted));
entityInformation.setId(instance, converted);
});

} catch (NonTransientDataAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BasicJdbcPersistentEntityInformation(JdbcPersistentEntity<T> persistentEn
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +25,7 @@
*/
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.
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down