Browse Source

DATAJDBC-130 - Polishing.

Formatting.
Tiny fixes in JavaDoc.
Fixed some type parameters.
Replaced concrete classes with interfaces.
pull/41/merge
Jens Schauder 8 years ago committed by Greg Turnquist
parent
commit
1603296385
No known key found for this signature in database
GPG Key ID: CB2FA4D512B5C413
  1. 18
      src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java
  2. 19
      src/main/java/org/springframework/data/jdbc/core/EntityRowMapper.java
  3. 10
      src/main/java/org/springframework/data/jdbc/core/IterableOfEntryToMapConverter.java
  4. 18
      src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java
  5. 12
      src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java
  6. 8
      src/main/java/org/springframework/data/jdbc/mapping/model/BasicJdbcPersistentProperty.java
  7. 48
      src/test/java/org/springframework/data/jdbc/core/EntityRowMapperUnitTests.java
  8. 2
      src/test/java/org/springframework/data/jdbc/core/IterableOfEntryToMapConverterUnitTests.java

18
src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java

@ -15,12 +15,6 @@
*/ */
package org.springframework.data.jdbc.core; package org.springframework.data.jdbc.core;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.NonTransientDataAccessException; import org.springframework.dao.NonTransientDataAccessException;
@ -40,6 +34,12 @@ import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder; import org.springframework.jdbc.support.KeyHolder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/** /**
* The default {@link DataAccessStrategy} is to generate SQL statements based on meta data from the entity. * The default {@link DataAccessStrategy} is to generate SQL statements based on meta data from the entity.
* *
@ -57,7 +57,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
private final DataAccessStrategy accessStrategy; private final DataAccessStrategy accessStrategy;
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, NamedParameterJdbcOperations operations, public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, NamedParameterJdbcOperations operations,
JdbcMappingContext context, DataAccessStrategy accessStrategy) { JdbcMappingContext context, DataAccessStrategy accessStrategy) {
this.sqlGeneratorSource = sqlGeneratorSource; this.sqlGeneratorSource = sqlGeneratorSource;
this.operations = operations; this.operations = operations;
@ -70,7 +70,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
* Only suitable if this is the only access strategy in use. * Only suitable if this is the only access strategy in use.
*/ */
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, NamedParameterJdbcOperations operations, public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, NamedParameterJdbcOperations operations,
JdbcMappingContext context) { JdbcMappingContext context) {
this.sqlGeneratorSource = sqlGeneratorSource; this.sqlGeneratorSource = sqlGeneratorSource;
this.operations = operations; this.operations = operations;
@ -97,7 +97,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
boolean idValueDoesNotComeFromEntity = // boolean idValueDoesNotComeFromEntity = //
idValue == null // idValue == null //
|| additionalParameters.containsKey(idProperty.getColumnName()); || additionalParameters.containsKey(idProperty.getColumnName());
operations.update( // operations.update( //
sql(domainType).getInsert(idValueDoesNotComeFromEntity, additionalParameters.keySet()), // sql(domainType).getInsert(idValueDoesNotComeFromEntity, additionalParameters.keySet()), //

19
src/main/java/org/springframework/data/jdbc/core/EntityRowMapper.java

@ -16,9 +16,6 @@
package org.springframework.data.jdbc.core; package org.springframework.data.jdbc.core;
import lombok.NonNull; import lombok.NonNull;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ClassGeneratingEntityInstantiator; import org.springframework.data.convert.ClassGeneratingEntityInstantiator;
@ -34,6 +31,9 @@ import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
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;
import java.sql.ResultSet;
import java.sql.SQLException;
/** /**
* Maps a ResultSet to an entity of type {@code T}, including entities referenced. * Maps a ResultSet to an entity of type {@code T}, including entities referenced.
* *
@ -53,7 +53,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
private final JdbcPersistentProperty idProperty; private final JdbcPersistentProperty idProperty;
public EntityRowMapper(JdbcPersistentEntity<T> entity, ConversionService conversions, JdbcMappingContext context, public EntityRowMapper(JdbcPersistentEntity<T> entity, ConversionService conversions, JdbcMappingContext context,
DataAccessStrategy accessStrategy) { DataAccessStrategy accessStrategy) {
this.entity = entity; this.entity = entity;
this.conversions = conversions; this.conversions = conversions;
@ -137,9 +137,12 @@ public class EntityRowMapper<T> implements RowMapper<T> {
private static class ResultSetParameterValueProvider implements ParameterValueProvider<JdbcPersistentProperty> { private static class ResultSetParameterValueProvider implements ParameterValueProvider<JdbcPersistentProperty> {
@NonNull private final ResultSet resultSet; @NonNull
@NonNull private final ConversionService conversionService; private final ResultSet resultSet;
@NonNull private final String prefix; @NonNull
private final ConversionService conversionService;
@NonNull
private final String prefix;
private ResultSetParameterValueProvider(ResultSet resultSet, ConversionService conversionService, String prefix) { private ResultSetParameterValueProvider(ResultSet resultSet, ConversionService conversionService, String prefix) {
@ -149,7 +152,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
} }
public static ResultSetParameterValueProvider of(ResultSet resultSet, ConversionService conversionService, public static ResultSetParameterValueProvider of(ResultSet resultSet, ConversionService conversionService,
String prefix) { String prefix) {
return new ResultSetParameterValueProvider(resultSet, conversionService, prefix); return new ResultSetParameterValueProvider(resultSet, conversionService, prefix);
} }

10
src/main/java/org/springframework/data/jdbc/core/IterableOfEntryToMapConverter.java

@ -15,16 +15,16 @@
*/ */
package org.springframework.data.jdbc.core; package org.springframework.data.jdbc.core;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter; import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/** /**
* A converter for creating a {@link Map} from an {@link Iterable<Map.Entry>}. * A converter for creating a {@link Map} from an {@link Iterable<Map.Entry>}.
* *
@ -36,7 +36,7 @@ class IterableOfEntryToMapConverter implements ConditionalConverter, Converter<I
@Override @Override
public Map convert(Iterable source) { public Map convert(Iterable source) {
HashMap result = new HashMap(); Map result = new HashMap();
source.forEach(element -> { source.forEach(element -> {

18
src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java

@ -15,14 +15,6 @@
*/ */
package org.springframework.data.jdbc.core; package org.springframework.data.jdbc.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext; import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
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;
@ -33,6 +25,14 @@ import org.springframework.data.util.Lazy;
import org.springframework.data.util.StreamUtils; import org.springframework.data.util.StreamUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* Generates SQL statements to be used by {@link SimpleJdbcRepository} * Generates SQL statements to be used by {@link SimpleJdbcRepository}
* *
@ -169,7 +169,7 @@ class SqlGenerator {
if (!property.isEntity() // if (!property.isEntity() //
|| Collection.class.isAssignableFrom(property.getType()) // || Collection.class.isAssignableFrom(property.getType()) //
|| Map.class.isAssignableFrom(property.getType()) // || Map.class.isAssignableFrom(property.getType()) //
) { ) {
continue; continue;
} }

12
src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java

@ -77,7 +77,7 @@ public class JdbcEntityWriter extends JdbcEntityWriterSupport {
} }
private void saveReferencedEntities(PropertyAndValue propertyAndValue, AggregateChange aggregateChange, private void saveReferencedEntities(PropertyAndValue propertyAndValue, AggregateChange aggregateChange,
JdbcPropertyPath propertyPath, DbAction dependingOn) { JdbcPropertyPath propertyPath, DbAction dependingOn) {
saveActions(propertyAndValue, propertyPath, dependingOn).forEach(a -> { saveActions(propertyAndValue, propertyPath, dependingOn).forEach(a -> {
@ -88,7 +88,7 @@ public class JdbcEntityWriter extends JdbcEntityWriterSupport {
} }
private Stream<DbAction> saveActions(PropertyAndValue propertyAndValue, JdbcPropertyPath propertyPath, private Stream<DbAction> saveActions(PropertyAndValue propertyAndValue, JdbcPropertyPath propertyPath,
DbAction dependingOn) { DbAction dependingOn) {
if (Map.Entry.class.isAssignableFrom(ClassUtils.getUserClass(propertyAndValue.value))) { if (Map.Entry.class.isAssignableFrom(ClassUtils.getUserClass(propertyAndValue.value))) {
return mapEntrySaveAction(propertyAndValue, propertyPath, dependingOn); return mapEntrySaveAction(propertyAndValue, propertyPath, dependingOn);
@ -98,7 +98,7 @@ public class JdbcEntityWriter extends JdbcEntityWriterSupport {
} }
private Stream<DbAction> mapEntrySaveAction(PropertyAndValue propertyAndValue, JdbcPropertyPath propertyPath, private Stream<DbAction> mapEntrySaveAction(PropertyAndValue propertyAndValue, JdbcPropertyPath propertyPath,
DbAction dependingOn) { DbAction dependingOn) {
Map.Entry<Object, Object> entry = (Map.Entry) propertyAndValue.value; Map.Entry<Object, Object> entry = (Map.Entry) propertyAndValue.value;
@ -117,7 +117,7 @@ public class JdbcEntityWriter extends JdbcEntityWriterSupport {
} }
private void insertReferencedEntities(PropertyAndValue propertyAndValue, AggregateChange aggregateChange, private void insertReferencedEntities(PropertyAndValue propertyAndValue, AggregateChange aggregateChange,
JdbcPropertyPath propertyPath, DbAction dependingOn) { JdbcPropertyPath propertyPath, DbAction dependingOn) {
Insert<Object> insert; Insert<Object> insert;
if (propertyAndValue.property.isQualified()) { if (propertyAndValue.property.isQualified()) {
@ -143,7 +143,7 @@ public class JdbcEntityWriter extends JdbcEntityWriterSupport {
.flatMap( // .flatMap( //
p -> referencedEntity(p, persistentEntity.getPropertyAccessor(o)) // p -> referencedEntity(p, persistentEntity.getPropertyAccessor(o)) //
.map(e -> new PropertyAndValue(p, e)) // .map(e -> new PropertyAndValue(p, e)) //
); );
} }
private Stream<Object> referencedEntity(JdbcPersistentProperty p, PersistentPropertyAccessor propertyAccessor) { private Stream<Object> referencedEntity(JdbcPersistentProperty p, PersistentPropertyAccessor propertyAccessor) {
@ -174,7 +174,7 @@ public class JdbcEntityWriter extends JdbcEntityWriterSupport {
} }
private Stream<Object> collectionPropertyAsStream(JdbcPersistentProperty p, private Stream<Object> collectionPropertyAsStream(JdbcPersistentProperty p,
PersistentPropertyAccessor propertyAccessor) { PersistentPropertyAccessor propertyAccessor) {
Object property = propertyAccessor.getProperty(p); Object property = propertyAccessor.getProperty(p);

8
src/main/java/org/springframework/data/jdbc/mapping/model/BasicJdbcPersistentProperty.java

@ -52,13 +52,13 @@ public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProper
/** /**
* Creates a new {@link AnnotationBasedPersistentProperty}. * Creates a new {@link AnnotationBasedPersistentProperty}.
* *
* @param property must not be {@literal null}. * @param property must not be {@literal null}.
* @param owner must not be {@literal null}. * @param owner must not be {@literal null}.
* @param simpleTypeHolder must not be {@literal null}. * @param simpleTypeHolder must not be {@literal null}.
* @param context must not be {@literal null} * @param context must not be {@literal null}
*/ */
public BasicJdbcPersistentProperty(Property property, PersistentEntity<?, JdbcPersistentProperty> owner, public BasicJdbcPersistentProperty(Property property, PersistentEntity<?, JdbcPersistentProperty> owner,
SimpleTypeHolder simpleTypeHolder, JdbcMappingContext context) { SimpleTypeHolder simpleTypeHolder, JdbcMappingContext context) {
super(property, owner, simpleTypeHolder); super(property, owner, simpleTypeHolder);

48
src/test/java/org/springframework/data/jdbc/core/EntityRowMapperUnitTests.java

@ -15,24 +15,7 @@
*/ */
package org.springframework.data.jdbc.core; package org.springframework.data.jdbc.core;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.naming.OperationNotSupportedException;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
@ -46,6 +29,21 @@ import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import javax.naming.OperationNotSupportedException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
/** /**
* Tests the extraction of entities from a {@link ResultSet} by the {@link EntityRowMapper}. * Tests the extraction of entities from a {@link ResultSet} by the {@link EntityRowMapper}.
* *
@ -137,7 +135,7 @@ public class EntityRowMapperUnitTests {
JdbcMappingContext context = new JdbcMappingContext(mock(NamedParameterJdbcOperations.class)); JdbcMappingContext context = new JdbcMappingContext(mock(NamedParameterJdbcOperations.class));
DataAccessStrategy accessStrategy = mock(DataAccessStrategy.class); DataAccessStrategy accessStrategy = mock(DataAccessStrategy.class);
// the ID of the entity is used to determin what kind of resultset is needed for subsequent selects. // the ID of the entity is used to determine what kind of ResultSet is needed for subsequent selects.
doReturn(new HashSet<>(asList(new Trivial(), new Trivial()))).when(accessStrategy).findAllByProperty(eq(ID_FOR_ENTITY_NOT_REFERENCING_MAP), doReturn(new HashSet<>(asList(new Trivial(), new Trivial()))).when(accessStrategy).findAllByProperty(eq(ID_FOR_ENTITY_NOT_REFERENCING_MAP),
any(JdbcPersistentProperty.class)); any(JdbcPersistentProperty.class));
@ -169,7 +167,7 @@ public class EntityRowMapperUnitTests {
"Number of values [%d] must be a multiple of the number of columns [%d]", // "Number of values [%d] must be a multiple of the number of columns [%d]", //
values.length, // values.length, //
columns.size() // columns.size() //
) // ) //
); );
List<Map<String, Object>> result = convertValues(columns, values); List<Map<String, Object>> result = convertValues(columns, values);
@ -255,14 +253,16 @@ public class EntityRowMapperUnitTests {
@RequiredArgsConstructor @RequiredArgsConstructor
static class Trivial { static class Trivial {
@Id Long id; @Id
Long id;
String name; String name;
} }
@RequiredArgsConstructor @RequiredArgsConstructor
static class OneToOne { static class OneToOne {
@Id Long id; @Id
Long id;
String name; String name;
Trivial child; Trivial child;
} }
@ -270,7 +270,8 @@ public class EntityRowMapperUnitTests {
@RequiredArgsConstructor @RequiredArgsConstructor
static class OneToSet { static class OneToSet {
@Id Long id; @Id
Long id;
String name; String name;
Set<Trivial> children; Set<Trivial> children;
} }
@ -278,7 +279,8 @@ public class EntityRowMapperUnitTests {
@RequiredArgsConstructor @RequiredArgsConstructor
static class OneToMap { static class OneToMap {
@Id Long id; @Id
Long id;
String name; String name;
Map<String, Trivial> children; Map<String, Trivial> children;
} }

2
src/test/java/org/springframework/data/jdbc/core/IterableOfEntryToMapConverterUnitTests.java

@ -39,7 +39,7 @@ public class IterableOfEntryToMapConverterUnitTests {
@Test @Test
public void testConversions() { public void testConversions() {
HashMap<Object, Object> map = new HashMap<>(); Map<Object, Object> map = new HashMap<>();
map.put("key", "value"); map.put("key", "value");
List<Object[]> testValues = asList( // List<Object[]> testValues = asList( //
new Object[] { emptySet(), emptyMap() }, // new Object[] { emptySet(), emptyMap() }, //

Loading…
Cancel
Save