Skip to content

Commit 61a54ca

Browse files
committed
DATAJDBC-331 - Polishing.
Simplify column name discovery using Optionals utility methods. Add test to verify Column annotation precedence. Javadoc, formatting. Original pull request: #117.
1 parent 6fcff3e commit 61a54ca

File tree

4 files changed

+61
-48
lines changed

4 files changed

+61
-48
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentPropertyUnitTests.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.core.mapping;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
1919

2020
import lombok.Data;
2121

@@ -27,6 +27,7 @@
2727

2828
import org.assertj.core.api.SoftAssertions;
2929
import org.junit.Test;
30+
3031
import org.springframework.data.annotation.Id;
3132
import org.springframework.data.mapping.PropertyHandler;
3233
import org.springframework.data.relational.core.mapping.BasicRelationalPersistentProperty;
@@ -42,6 +43,7 @@
4243
* @author Jens Schauder
4344
* @author Oliver Gierke
4445
* @author Florian Lüdiger
46+
* @author Mark Paluch
4547
*/
4648
public class BasicJdbcPersistentPropertyUnitTests {
4749

@@ -51,8 +53,6 @@ public class BasicJdbcPersistentPropertyUnitTests {
5153
@Test // DATAJDBC-104
5254
public void enumGetsStoredAsString() {
5355

54-
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
55-
5656
entity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) p -> {
5757
switch (p.getName()) {
5858
case "someEnum":
@@ -118,6 +118,28 @@ public void referencesAreNotEntitiesAndGetStoredAsTheirId() {
118118
softly.assertAll();
119119
}
120120

121+
@Test // DATAJDBC-331
122+
public void detectsKeyColumnNameFromColumnAnnotation() {
123+
124+
RelationalPersistentProperty listProperty = context //
125+
.getRequiredPersistentEntity(WithCollections.class) //
126+
.getRequiredPersistentProperty("someList");
127+
128+
assertThat(listProperty.getKeyColumn()).isEqualTo("some_key");
129+
assertThat(listProperty.getReverseColumnName()).isEqualTo("some_value");
130+
}
131+
132+
@Test // DATAJDBC-331
133+
public void detectsKeyColumnOverrideNameFromMappedCollectionAnnotation() {
134+
135+
RelationalPersistentProperty listProperty = context //
136+
.getRequiredPersistentEntity(WithCollections.class) //
137+
.getRequiredPersistentProperty("overrideList");
138+
139+
assertThat(listProperty.getKeyColumn()).isEqualTo("override_key");
140+
assertThat(listProperty.getReverseColumnName()).isEqualTo("override_id");
141+
}
142+
121143
private void checkTargetType(SoftAssertions softly, RelationalPersistentEntity<?> persistentEntity,
122144
String propertyName, Class<?> expected) {
123145

@@ -138,7 +160,8 @@ private static class DummyEntity {
138160
private final List<String> listField;
139161
private final UUID uuid;
140162

141-
@MappedCollection(idColumn = "dummy_column_name", keyColumn = "dummy_key_column_name") private List<Integer> someList;
163+
@MappedCollection(idColumn = "dummy_column_name",
164+
keyColumn = "dummy_key_column_name") private List<Integer> someList;
142165

143166
// DATACMNS-106
144167
private @Column("dummy_name") String name;
@@ -157,6 +180,14 @@ public List<Date> getListGetter() {
157180
}
158181
}
159182

183+
@Data
184+
private static class WithCollections {
185+
186+
@Column(value = "some_value", keyColumn = "some_key") List<Integer> someList;
187+
@Column(value = "some_value", keyColumn = "some_key") @MappedCollection(idColumn = "override_id",
188+
keyColumn = "override_key") List<Integer> overrideList;
189+
}
190+
160191
@SuppressWarnings("unused")
161192
private enum SomeEnum {
162193
ALPHA

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
import java.util.Date;
2222
import java.util.LinkedHashMap;
2323
import java.util.Map;
24-
import java.util.Objects;
2524
import java.util.Optional;
2625
import java.util.Set;
27-
import java.util.stream.Stream;
2826

2927
import org.springframework.data.mapping.Association;
3028
import org.springframework.data.mapping.PersistentEntity;
3129
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
3230
import org.springframework.data.mapping.model.Property;
3331
import org.springframework.data.mapping.model.SimpleTypeHolder;
3432
import org.springframework.data.util.Lazy;
33+
import org.springframework.data.util.Optionals;
3534
import org.springframework.lang.Nullable;
3635
import org.springframework.util.Assert;
3736
import org.springframework.util.ClassUtils;
@@ -88,41 +87,19 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
8887
.map(Embedded::value) //
8988
.orElse(""));
9089

91-
this.columnName = Lazy.of(() -> Optional.ofNullable( //
92-
findAnnotation(Column.class)) //
90+
this.columnName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Column.class)) //
9391
.map(Column::value) //
94-
.filter(StringUtils::hasText)//
95-
);
96-
97-
this.collectionIdColumnName = Lazy.of(() ->
98-
Stream.concat( //
99-
Stream.of( //
100-
findAnnotation(MappedCollection.class)) //
101-
.filter(Objects::nonNull) //
102-
.map(MappedCollection::idColumn), //
103-
Stream.of( //
104-
findAnnotation(Column.class)) //
105-
.filter(Objects::nonNull) //
106-
.map(Column::value) //
107-
)
108-
.filter(StringUtils::hasText)
109-
.findFirst()
110-
);
111-
112-
this.collectionKeyColumnName = Lazy.of(() ->
113-
Stream.concat( //
114-
Stream.of( //
115-
findAnnotation(MappedCollection.class)) //
116-
.filter(Objects::nonNull) //
117-
.map(MappedCollection::keyColumn), //
118-
Stream.of( //
119-
findAnnotation(Column.class)) //
120-
.filter(Objects::nonNull) //
121-
.map(Column::keyColumn) //
122-
)
123-
.filter(StringUtils::hasText)
124-
.findFirst()
125-
);
92+
.filter(StringUtils::hasText));
93+
94+
this.collectionIdColumnName = Lazy.of(() -> Optionals
95+
.toStream(Optional.ofNullable(findAnnotation(MappedCollection.class)).map(MappedCollection::idColumn),
96+
Optional.ofNullable(findAnnotation(Column.class)).map(Column::value)) //
97+
.filter(StringUtils::hasText).findFirst());
98+
99+
this.collectionKeyColumnName = Lazy.of(() -> Optionals
100+
.toStream(Optional.ofNullable(findAnnotation(MappedCollection.class)).map(MappedCollection::keyColumn),
101+
Optional.ofNullable(findAnnotation(Column.class)).map(Column::keyColumn)) //
102+
.filter(StringUtils::hasText).findFirst());
126103
}
127104

128105
/*

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/MappedCollection.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,34 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23+
import java.util.List;
24+
import java.util.Map;
2325

2426
/**
25-
* The annotation to configure the mapping from an collection in the database.
27+
* The annotation to configure the mapping for a {@link List} or {@link Map} property in the database.
2628
*
2729
* @since 1.1
2830
* @author Bastian Wilhelm
31+
* @author Mark Paluch
2932
*/
3033
@Retention(RetentionPolicy.RUNTIME)
3134
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
3235
@Documented
3336
public @interface MappedCollection {
3437

3538
/**
36-
* The column name for id column in the corresponding relationship table.
37-
* If the default value (empty String) is used, the column name is resolved by the used
38-
* {@link NamingStrategy} method {@link NamingStrategy#getReverseColumnName(RelationalPersistentProperty)}
39+
* The column name for id column in the corresponding relationship table. Defaults to {@link NamingStrategy} usage if
40+
* the value is empty.
41+
*
42+
* @see NamingStrategy#getReverseColumnName(RelationalPersistentProperty)
3943
*/
4044
String idColumn() default "";
4145

4246
/**
43-
* The column name for key columns of List or Map collections in the corresponding relationship table.
44-
* If the default value (empty String) is used, the column name is resolved by the used
45-
* {@link NamingStrategy} method {@link NamingStrategy#getKeyColumn(RelationalPersistentProperty)}
47+
* The column name for key columns of {@link List} or {@link Map} collections in the corresponding relationship table.
48+
* Defaults to {@link NamingStrategy} usage if the value is empty.
49+
*
50+
* @see NamingStrategy#getKeyColumn(RelationalPersistentProperty)
4651
*/
4752
String keyColumn() default "";
4853
}

src/main/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= Spring Data JDBC - Reference Documentation
2-
Jens Schauder, Jay Bryant, Mark Paluch
2+
Jens Schauder, Jay Bryant, Mark Paluch, Bastian Wilhelm
33
:revnumber: {version}
44
:revdate: {localdate}
55
:javadoc-base: https://docs.spring.io/spring-data/jdbc/docs/{revnumber}/api/

0 commit comments

Comments
 (0)