Skip to content

Commit fe5ae93

Browse files
committed
DATAJDBC-107 - Polishing.
Code formatting. Added @author tag. Tiny code simplifications.
1 parent f7bff01 commit fe5ae93

File tree

9 files changed

+64
-58
lines changed

9 files changed

+64
-58
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
2727
import org.springframework.data.mapping.model.Property;
2828
import org.springframework.data.mapping.model.SimpleTypeHolder;
29+
import org.springframework.util.Assert;
2930
import org.springframework.util.ClassUtils;
3031

3132
/**
3233
* Meta data about a property to be used by repository implementations.
3334
*
3435
* @author Jens Schauder
36+
* @author Greg Turnquist
3537
* @since 2.0
3638
*/
3739
public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProperty<JdbcPersistentProperty>
@@ -52,11 +54,15 @@ public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProper
5254
* @param property must not be {@literal null}.
5355
* @param owner must not be {@literal null}.
5456
* @param simpleTypeHolder must not be {@literal null}.
55-
* @param context
57+
* @param context must not be {@literal null}
5658
*/
5759
public BasicJdbcPersistentProperty(Property property, PersistentEntity<?, JdbcPersistentProperty> owner,
5860
SimpleTypeHolder simpleTypeHolder, JdbcMappingContext context) {
61+
5962
super(property, owner, simpleTypeHolder);
63+
64+
Assert.notNull(context, "context must not be null.");
65+
6066
this.context = context;
6167
}
6268

src/main/java/org/springframework/data/jdbc/mapping/model/JdbcMappingContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* {@link MappingContext} implementation for JDBC.
3939
*
4040
* @author Jens Schauder
41+
* @author Greg Turnquist
4142
* @since 2.0
4243
*/
4344
public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEntity<?>, JdbcPersistentProperty> {
@@ -63,8 +64,6 @@ public List<PropertyPath> referencedEntities(Class<?> rootType, PropertyPath pat
6364
Class<?> currentType = path == null ? rootType : PropertyPaths.getLeafType(path);
6465
JdbcPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(currentType);
6566

66-
String rootPrefix = path == null ? "" : path.toDotPath() + ".";
67-
6867
for (JdbcPersistentProperty property : persistentEntity) {
6968
if (property.isEntity()) {
7069

src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentEntityImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Meta data a repository might need for implementing persistence operations for instances of type {@code T}
2525
*
2626
* @author Jens Schauder
27+
* @author Greg Turnquist
2728
* @since 2.0
2829
*/
2930
class JdbcPersistentEntityImpl<T> extends BasicPersistentEntity<T, JdbcPersistentProperty>

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
/**
3333
* @author Jens Schauder
34+
* @author Greg Turnquist
3435
* @since 2.0
3536
*/
3637
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
@@ -39,7 +40,8 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
3940
private final NamedParameterJdbcOperations jdbcOperations;
4041
private final ApplicationEventPublisher publisher;
4142

42-
public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations, ApplicationEventPublisher publisher, NamingStrategy namingStrategy) {
43+
public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations,
44+
ApplicationEventPublisher publisher, NamingStrategy namingStrategy) {
4345

4446
this.jdbcOperations = namedParameterJdbcOperations;
4547
this.publisher = publisher;

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* repository factories via Spring configuration.
3939
*
4040
* @author Jens Schauder
41+
* @author Greg Turnquist
4142
* @since 2.0
4243
*/
4344
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
4748
"No unique NamedParameterJdbcOperation could be found, " //
4849
+ "nor JdbcOperations or DataSource to construct one from.";
4950

50-
private static final String NO_NAMING_STRATEGY_ERROR_MESSAGE = "No unique NamingStrategy could be found.";
51-
5251
private static final String NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate";
5352
private static final String JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate";
5453
private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
@@ -67,23 +66,21 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
6766

6867
@Override
6968
protected RepositoryFactorySupport doCreateRepositoryFactory() {
70-
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher, findOrCreateNamingStrategy());
69+
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher,
70+
findOrCreateNamingStrategy());
7171
}
7272

7373
private NamedParameterJdbcOperations findOrCreateJdbcOperations() {
7474

75-
return Optionals
76-
.firstNonEmpty( //
77-
this::getNamedParameterJdbcOperations, //
78-
() -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), //
79-
() -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
75+
return Optionals.firstNonEmpty( //
76+
this::getNamedParameterJdbcOperations, //
77+
() -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), //
78+
() -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
8079
.orElseThrow(() -> new IllegalStateException(NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE));
8180
}
8281

8382
private NamingStrategy findOrCreateNamingStrategy() {
84-
85-
return getNamingStrategy()
86-
.orElse(new DefaultNamingStrategy());
83+
return getNamingStrategy().orElse(new DefaultNamingStrategy());
8784
}
8885

8986
private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() {

src/test/java/org/springframework/data/jdbc/core/SqlGeneratorContextBasedNamingStrategyUnitTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,23 @@ private SqlGenerator configureSqlGenerator(NamingStrategy namingStrategy) {
191191
return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
192192
}
193193

194+
@SuppressWarnings("unused")
194195
static class DummyEntity {
195196

196197
@Id Long id;
197198
String name;
198199
ReferencedEntity ref;
199200
}
200201

202+
@SuppressWarnings("unused")
201203
static class ReferencedEntity {
202204

203205
@Id Long l1id;
204206
String content;
205207
SecondLevelReferencedEntity further;
206208
}
207209

210+
@SuppressWarnings("unused")
208211
static class SecondLevelReferencedEntity {
209212

210213
@Id Long l2id;

src/test/java/org/springframework/data/jdbc/core/SqlGeneratorFixedNamingStrategyUnitTests.java

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@
2828
import org.springframework.data.mapping.PropertyPath;
2929

3030
/**
31-
* Unit tests to a fixed {@link NamingStrategy} implementation containing a hard wired schema, table, and property prefix.
32-
*
33-
* NOTE: Due to the need to verify SQL generation and {@link SqlGenerator}'s package-private status suggests
34-
* this unit test exist in this package, not {@literal org.springframework.data.jdbc.mappings.model}.
31+
* Unit tests the {@link SqlGenerator} with a fixed {@link NamingStrategy} implementation containing a hard wired
32+
* schema, table, and property prefix.
3533
*
3634
* @author Greg Turnquist
3735
*/
@@ -61,7 +59,7 @@ public String getColumnName(JdbcPersistentProperty property) {
6159
public String getTableName(Class<?> type) {
6260
return type.getSimpleName().toUpperCase();
6361
}
64-
62+
6563
@Override
6664
public String getColumnName(JdbcPersistentProperty property) {
6765
return property.getName().toLowerCase();
@@ -77,12 +75,14 @@ public void findOneWithOverriddenFixedTableName() {
7775

7876
SoftAssertions softAssertions = new SoftAssertions();
7977
softAssertions.assertThat(sql) //
80-
.startsWith("SELECT") //
81-
.contains("FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_id AS FixedCustomPropertyPrefix_id,") //
82-
.contains("FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_name AS FixedCustomPropertyPrefix_name,") //
83-
.contains("ref.FixedCustomPropertyPrefix_l1id AS ref_FixedCustomPropertyPrefix_l1id") //
84-
.contains("ref.FixedCustomPropertyPrefix_content AS ref_FixedCustomPropertyPrefix_content") //
85-
.contains("FROM FixedCustomSchema.FixedCustomTablePrefix_DummyEntity");
78+
.startsWith("SELECT") //
79+
.contains(
80+
"FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_id AS FixedCustomPropertyPrefix_id,") //
81+
.contains(
82+
"FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_name AS FixedCustomPropertyPrefix_name,") //
83+
.contains("ref.FixedCustomPropertyPrefix_l1id AS ref_FixedCustomPropertyPrefix_l1id") //
84+
.contains("ref.FixedCustomPropertyPrefix_content AS ref_FixedCustomPropertyPrefix_content") //
85+
.contains("FROM FixedCustomSchema.FixedCustomTablePrefix_DummyEntity");
8686
softAssertions.assertAll();
8787
}
8888

@@ -95,12 +95,12 @@ public void findOneWithUppercasedTablesAndLowercasedColumns() {
9595

9696
SoftAssertions softAssertions = new SoftAssertions();
9797
softAssertions.assertThat(sql) //
98-
.startsWith("SELECT") //
99-
.contains("DUMMYENTITY.id AS id,") //
100-
.contains("DUMMYENTITY.name AS name,") //
101-
.contains("ref.l1id AS ref_l1id") //
102-
.contains("ref.content AS ref_content") //
103-
.contains("FROM DUMMYENTITY");
98+
.startsWith("SELECT") //
99+
.contains("DUMMYENTITY.id AS id,") //
100+
.contains("DUMMYENTITY.name AS name,") //
101+
.contains("ref.l1id AS ref_l1id") //
102+
.contains("ref.content AS ref_content") //
103+
.contains("FROM DUMMYENTITY");
104104
softAssertions.assertAll();
105105
}
106106

@@ -111,8 +111,8 @@ public void cascadingDeleteFirstLevel() {
111111

112112
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref", DummyEntity.class));
113113

114-
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
115-
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId");
114+
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
115+
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId");
116116
}
117117

118118
@Test // DATAJDBC-107
@@ -122,12 +122,10 @@ public void cascadingDeleteAllSecondLevel() {
122122

123123
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref.further", DummyEntity.class));
124124

125-
assertThat(sql).isEqualTo(
126-
"DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity " +
127-
"WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN " +
128-
"(SELECT FixedCustomPropertyPrefix_l1id " +
129-
"FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
130-
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId)");
125+
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity "
126+
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN "
127+
+ "(SELECT FixedCustomPropertyPrefix_l1id " + "FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
128+
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId)");
131129
}
132130

133131
@Test // DATAJDBC-107
@@ -147,8 +145,8 @@ public void cascadingDeleteAllFirstLevel() {
147145

148146
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref", DummyEntity.class));
149147

150-
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
151-
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL");
148+
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
149+
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL");
152150
}
153151

154152
@Test // DATAJDBC-107
@@ -158,12 +156,10 @@ public void cascadingDeleteSecondLevel() {
158156

159157
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref.further", DummyEntity.class));
160158

161-
assertThat(sql).isEqualTo(
162-
"DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity " +
163-
"WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN " +
164-
"(SELECT FixedCustomPropertyPrefix_l1id " +
165-
"FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
166-
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL)");
159+
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity "
160+
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN "
161+
+ "(SELECT FixedCustomPropertyPrefix_l1id " + "FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
162+
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL)");
167163
}
168164

169165
/**

src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,18 @@
3131
* Unit tests for the {@link SqlGenerator}.
3232
*
3333
* @author Jens Schauder
34+
* @author Greg Turnquist
3435
*/
3536
public class SqlGeneratorUnitTests {
3637

37-
private NamingStrategy namingStrategy;
38-
private JdbcMappingContext context;
39-
private JdbcPersistentEntity<?> persistentEntity;
4038
private SqlGenerator sqlGenerator;
4139

4240
@Before
4341
public void setUp() {
4442

45-
this.namingStrategy = new DefaultNamingStrategy();
46-
this.context = new JdbcMappingContext(namingStrategy);
47-
this.persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
43+
NamingStrategy namingStrategy = new DefaultNamingStrategy();
44+
JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
45+
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
4846
this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
4947
}
5048

@@ -105,20 +103,23 @@ public void cascadingDeleteSecondLevel() {
105103
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT l1id FROM ReferencedEntity WHERE DummyEntity IS NOT NULL)");
106104
}
107105

106+
@SuppressWarnings("unused")
108107
static class DummyEntity {
109108

110109
@Id Long id;
111110
String name;
112111
ReferencedEntity ref;
113112
}
114113

114+
@SuppressWarnings("unused")
115115
static class ReferencedEntity {
116116

117117
@Id Long l1id;
118118
String content;
119119
SecondLevelReferencedEntity further;
120120
}
121121

122+
@SuppressWarnings("unused")
122123
static class SecondLevelReferencedEntity {
123124

124125
@Id Long l2id;

src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Testing special cases for id generation with {@link SimpleJdbcRepository}.
4848
*
4949
* @author Jens Schauder
50+
* @author Greg Turnquist
5051
*/
5152
@ContextConfiguration
5253
@EnableJdbcRepositories(considerNestedRepositories = true)
@@ -131,8 +132,8 @@ Class<?> testClass() {
131132
}
132133

133134
/**
134-
* {@link NamingStrategy} that harmlessly uppercases the table name,
135-
* demonstrating how to inject one while not breaking existing SQL operations.
135+
* {@link NamingStrategy} that harmlessly uppercases the table name, demonstrating how to inject one while not
136+
* breaking existing SQL operations.
136137
*/
137138
@Bean
138139
NamingStrategy namingStrategy() {
@@ -152,8 +153,8 @@ NamedParameterJdbcTemplate template(DataSource db) {
152153
@Bean
153154
ReadOnlyIdEntityRepository readOnlyIdRepository(DataSource db, NamingStrategy namingStrategy) {
154155

155-
return new JdbcRepositoryFactory(new NamedParameterJdbcTemplate(db), mock(ApplicationEventPublisher.class), namingStrategy)
156-
.getRepository(ReadOnlyIdEntityRepository.class);
156+
return new JdbcRepositoryFactory(new NamedParameterJdbcTemplate(db), mock(ApplicationEventPublisher.class),
157+
namingStrategy).getRepository(ReadOnlyIdEntityRepository.class);
157158
}
158159

159160
@Bean

0 commit comments

Comments
 (0)