DATAJDBC-107 - Polishing.

Code formatting.
Added @author tag.
Tiny code simplifications.
This commit is contained in:
Jens Schauder
2017-08-14 08:57:05 +02:00
parent f7bff01d3e
commit fe5ae93fd0
9 changed files with 64 additions and 58 deletions

View File

@@ -26,12 +26,14 @@ import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
* Meta data about a property to be used by repository implementations. * Meta data about a property to be used by repository implementations.
* *
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
* @since 2.0 * @since 2.0
*/ */
public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProperty<JdbcPersistentProperty> public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProperty<JdbcPersistentProperty>
@@ -52,11 +54,15 @@ public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProper
* @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 * @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);
Assert.notNull(context, "context must not be null.");
this.context = context; this.context = context;
} }

View File

@@ -38,6 +38,7 @@ import org.springframework.data.util.TypeInformation;
* {@link MappingContext} implementation for JDBC. * {@link MappingContext} implementation for JDBC.
* *
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
* @since 2.0 * @since 2.0
*/ */
public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEntity<?>, JdbcPersistentProperty> { public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEntity<?>, JdbcPersistentProperty> {
@@ -63,8 +64,6 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
Class<?> currentType = path == null ? rootType : PropertyPaths.getLeafType(path); Class<?> currentType = path == null ? rootType : PropertyPaths.getLeafType(path);
JdbcPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(currentType); JdbcPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(currentType);
String rootPrefix = path == null ? "" : path.toDotPath() + ".";
for (JdbcPersistentProperty property : persistentEntity) { for (JdbcPersistentProperty property : persistentEntity) {
if (property.isEntity()) { if (property.isEntity()) {

View File

@@ -24,6 +24,7 @@ import org.springframework.data.util.TypeInformation;
* Meta data a repository might need for implementing persistence operations for instances of type {@code T} * Meta data a repository might need for implementing persistence operations for instances of type {@code T}
* *
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
* @since 2.0 * @since 2.0
*/ */
class JdbcPersistentEntityImpl<T> extends BasicPersistentEntity<T, JdbcPersistentProperty> class JdbcPersistentEntityImpl<T> extends BasicPersistentEntity<T, JdbcPersistentProperty>

View File

@@ -31,6 +31,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
/** /**
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
* @since 2.0 * @since 2.0
*/ */
public class JdbcRepositoryFactory extends RepositoryFactorySupport { public class JdbcRepositoryFactory extends RepositoryFactorySupport {
@@ -39,7 +40,8 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
private final NamedParameterJdbcOperations jdbcOperations; private final NamedParameterJdbcOperations jdbcOperations;
private final ApplicationEventPublisher publisher; private final ApplicationEventPublisher publisher;
public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations, ApplicationEventPublisher publisher, NamingStrategy namingStrategy) { public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations,
ApplicationEventPublisher publisher, NamingStrategy namingStrategy) {
this.jdbcOperations = namedParameterJdbcOperations; this.jdbcOperations = namedParameterJdbcOperations;
this.publisher = publisher; this.publisher = publisher;

View File

@@ -38,6 +38,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
* repository factories via Spring configuration. * repository factories via Spring configuration.
* *
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
* @since 2.0 * @since 2.0
*/ */
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> // public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> //
@@ -47,8 +48,6 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
"No unique NamedParameterJdbcOperation could be found, " // "No unique NamedParameterJdbcOperation could be found, " //
+ "nor JdbcOperations or DataSource to construct one from."; + "nor JdbcOperations or DataSource to construct one from.";
private static final String NO_NAMING_STRATEGY_ERROR_MESSAGE = "No unique NamingStrategy could be found.";
private static final String NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate"; private static final String NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate";
private static final String JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate"; private static final String JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate";
private static final String DATA_SOURCE_BEAN_NAME = "dataSource"; private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
@@ -67,23 +66,21 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
@Override @Override
protected RepositoryFactorySupport doCreateRepositoryFactory() { protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher, findOrCreateNamingStrategy()); return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher,
findOrCreateNamingStrategy());
} }
private NamedParameterJdbcOperations findOrCreateJdbcOperations() { private NamedParameterJdbcOperations findOrCreateJdbcOperations() {
return Optionals return Optionals.firstNonEmpty( //
.firstNonEmpty( // this::getNamedParameterJdbcOperations, //
this::getNamedParameterJdbcOperations, // () -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), //
() -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), // () -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
() -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
.orElseThrow(() -> new IllegalStateException(NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE)); .orElseThrow(() -> new IllegalStateException(NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE));
} }
private NamingStrategy findOrCreateNamingStrategy() { private NamingStrategy findOrCreateNamingStrategy() {
return getNamingStrategy().orElse(new DefaultNamingStrategy());
return getNamingStrategy()
.orElse(new DefaultNamingStrategy());
} }
private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() { private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() {

View File

@@ -191,6 +191,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests {
return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context)); return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
} }
@SuppressWarnings("unused")
static class DummyEntity { static class DummyEntity {
@Id Long id; @Id Long id;
@@ -198,6 +199,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests {
ReferencedEntity ref; ReferencedEntity ref;
} }
@SuppressWarnings("unused")
static class ReferencedEntity { static class ReferencedEntity {
@Id Long l1id; @Id Long l1id;
@@ -205,6 +207,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests {
SecondLevelReferencedEntity further; SecondLevelReferencedEntity further;
} }
@SuppressWarnings("unused")
static class SecondLevelReferencedEntity { static class SecondLevelReferencedEntity {
@Id Long l2id; @Id Long l2id;

View File

@@ -28,10 +28,8 @@ import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.data.mapping.PropertyPath; import org.springframework.data.mapping.PropertyPath;
/** /**
* Unit tests to a fixed {@link NamingStrategy} implementation containing a hard wired schema, table, and property prefix. * Unit tests the {@link SqlGenerator} with a fixed {@link NamingStrategy} implementation containing a hard wired
* * schema, table, and property prefix.
* NOTE: Due to the need to verify SQL generation and {@link SqlGenerator}'s package-private status suggests
* this unit test exist in this package, not {@literal org.springframework.data.jdbc.mappings.model}.
* *
* @author Greg Turnquist * @author Greg Turnquist
*/ */
@@ -77,12 +75,14 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
SoftAssertions softAssertions = new SoftAssertions(); SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(sql) // softAssertions.assertThat(sql) //
.startsWith("SELECT") // .startsWith("SELECT") //
.contains("FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_id AS FixedCustomPropertyPrefix_id,") // .contains(
.contains("FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_name AS FixedCustomPropertyPrefix_name,") // "FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_id AS FixedCustomPropertyPrefix_id,") //
.contains("ref.FixedCustomPropertyPrefix_l1id AS ref_FixedCustomPropertyPrefix_l1id") // .contains(
.contains("ref.FixedCustomPropertyPrefix_content AS ref_FixedCustomPropertyPrefix_content") // "FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_name AS FixedCustomPropertyPrefix_name,") //
.contains("FROM FixedCustomSchema.FixedCustomTablePrefix_DummyEntity"); .contains("ref.FixedCustomPropertyPrefix_l1id AS ref_FixedCustomPropertyPrefix_l1id") //
.contains("ref.FixedCustomPropertyPrefix_content AS ref_FixedCustomPropertyPrefix_content") //
.contains("FROM FixedCustomSchema.FixedCustomTablePrefix_DummyEntity");
softAssertions.assertAll(); softAssertions.assertAll();
} }
@@ -95,12 +95,12 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
SoftAssertions softAssertions = new SoftAssertions(); SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(sql) // softAssertions.assertThat(sql) //
.startsWith("SELECT") // .startsWith("SELECT") //
.contains("DUMMYENTITY.id AS id,") // .contains("DUMMYENTITY.id AS id,") //
.contains("DUMMYENTITY.name AS name,") // .contains("DUMMYENTITY.name AS name,") //
.contains("ref.l1id AS ref_l1id") // .contains("ref.l1id AS ref_l1id") //
.contains("ref.content AS ref_content") // .contains("ref.content AS ref_content") //
.contains("FROM DUMMYENTITY"); .contains("FROM DUMMYENTITY");
softAssertions.assertAll(); softAssertions.assertAll();
} }
@@ -111,8 +111,8 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref", DummyEntity.class)); String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref", DummyEntity.class));
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " + assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId"); + "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId");
} }
@Test // DATAJDBC-107 @Test // DATAJDBC-107
@@ -122,12 +122,10 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref.further", DummyEntity.class)); String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref.further", DummyEntity.class));
assertThat(sql).isEqualTo( assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity "
"DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity " + + "WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN "
"WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN " + + "(SELECT FixedCustomPropertyPrefix_l1id " + "FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
"(SELECT FixedCustomPropertyPrefix_l1id " + + "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId)");
"FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId)");
} }
@Test // DATAJDBC-107 @Test // DATAJDBC-107
@@ -147,8 +145,8 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref", DummyEntity.class)); String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref", DummyEntity.class));
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " + assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL"); + "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL");
} }
@Test // DATAJDBC-107 @Test // DATAJDBC-107
@@ -158,12 +156,10 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref.further", DummyEntity.class)); String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref.further", DummyEntity.class));
assertThat(sql).isEqualTo( assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity "
"DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity " + + "WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN "
"WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN " + + "(SELECT FixedCustomPropertyPrefix_l1id " + "FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
"(SELECT FixedCustomPropertyPrefix_l1id " + + "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL)");
"FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL)");
} }
/** /**

View File

@@ -31,20 +31,18 @@ import org.springframework.data.mapping.PropertyPath;
* Unit tests for the {@link SqlGenerator}. * Unit tests for the {@link SqlGenerator}.
* *
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
*/ */
public class SqlGeneratorUnitTests { public class SqlGeneratorUnitTests {
private NamingStrategy namingStrategy;
private JdbcMappingContext context;
private JdbcPersistentEntity<?> persistentEntity;
private SqlGenerator sqlGenerator; private SqlGenerator sqlGenerator;
@Before @Before
public void setUp() { public void setUp() {
this.namingStrategy = new DefaultNamingStrategy(); NamingStrategy namingStrategy = new DefaultNamingStrategy();
this.context = new JdbcMappingContext(namingStrategy); JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
this.persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class); JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context)); this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
} }
@@ -105,6 +103,7 @@ public class SqlGeneratorUnitTests {
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT l1id FROM ReferencedEntity WHERE DummyEntity IS NOT NULL)"); "DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT l1id FROM ReferencedEntity WHERE DummyEntity IS NOT NULL)");
} }
@SuppressWarnings("unused")
static class DummyEntity { static class DummyEntity {
@Id Long id; @Id Long id;
@@ -112,6 +111,7 @@ public class SqlGeneratorUnitTests {
ReferencedEntity ref; ReferencedEntity ref;
} }
@SuppressWarnings("unused")
static class ReferencedEntity { static class ReferencedEntity {
@Id Long l1id; @Id Long l1id;
@@ -119,6 +119,7 @@ public class SqlGeneratorUnitTests {
SecondLevelReferencedEntity further; SecondLevelReferencedEntity further;
} }
@SuppressWarnings("unused")
static class SecondLevelReferencedEntity { static class SecondLevelReferencedEntity {
@Id Long l2id; @Id Long l2id;

View File

@@ -47,6 +47,7 @@ import org.springframework.test.context.junit4.rules.SpringMethodRule;
* Testing special cases for id generation with {@link SimpleJdbcRepository}. * Testing special cases for id generation with {@link SimpleJdbcRepository}.
* *
* @author Jens Schauder * @author Jens Schauder
* @author Greg Turnquist
*/ */
@ContextConfiguration @ContextConfiguration
@EnableJdbcRepositories(considerNestedRepositories = true) @EnableJdbcRepositories(considerNestedRepositories = true)
@@ -131,8 +132,8 @@ public class JdbcRepositoryIdGenerationIntegrationTests {
} }
/** /**
* {@link NamingStrategy} that harmlessly uppercases the table name, * {@link NamingStrategy} that harmlessly uppercases the table name, demonstrating how to inject one while not
* demonstrating how to inject one while not breaking existing SQL operations. * breaking existing SQL operations.
*/ */
@Bean @Bean
NamingStrategy namingStrategy() { NamingStrategy namingStrategy() {
@@ -152,8 +153,8 @@ public class JdbcRepositoryIdGenerationIntegrationTests {
@Bean @Bean
ReadOnlyIdEntityRepository readOnlyIdRepository(DataSource db, NamingStrategy namingStrategy) { ReadOnlyIdEntityRepository readOnlyIdRepository(DataSource db, NamingStrategy namingStrategy) {
return new JdbcRepositoryFactory(new NamedParameterJdbcTemplate(db), mock(ApplicationEventPublisher.class), namingStrategy) return new JdbcRepositoryFactory(new NamedParameterJdbcTemplate(db), mock(ApplicationEventPublisher.class),
.getRepository(ReadOnlyIdEntityRepository.class); namingStrategy).getRepository(ReadOnlyIdEntityRepository.class);
} }
@Bean @Bean