Browse Source

DATAJDBC-218 - The Column annotation affects the back reference too.

Also:
Added documentation for the new behavior.
Made the value of the column annotation optional by providing a default.

Original pull request: #83.
pull/82/merge
Jens Schauder 7 years ago
parent
commit
08ee846e71
  1. 3
      src/main/asciidoc/jdbc.adoc
  2. 10
      src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java
  3. 2
      src/main/java/org/springframework/data/relational/core/mapping/Column.java
  4. 2
      src/test/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentPropertyUnitTests.java

3
src/main/asciidoc/jdbc.adoc

@ -131,7 +131,8 @@ You can change this name by implementing `NamingStrategy.getReverseColumnName(Re @@ -131,7 +131,8 @@ You can change this name by implementing `NamingStrategy.getReverseColumnName(Re
* `Map<simple type, some entity>` is considered a qualified one-to-many relationship.
The table of the referenced entity is expected to have two additional columns: One named the same as the table of the referencing entity for the foreign key and one with the same name and an additional `_key` suffix for the map key.
You can change this name by implementing `NamingStrategy.getReverseColumnName(RelationalPersistentProperty property)` and `NamingStrategy.getKeyColumn(RelationalPersistentProperty property)`, respectively.
You can change this behavior by implementing `NamingStrategy.getReverseColumnName(RelationalPersistentProperty property)` and `NamingStrategy.getKeyColumn(RelationalPersistentProperty property)`, respectively.
Alternatively you may annotate the attribute with `@Column(value="your_column_name", keyColumn="your_key_column_name")`
* `List<some entity>` is mapped as a `Map<Integer, some entity>`.

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

@ -73,7 +73,13 @@ class BasicRelationalPersistentProperty extends AnnotationBasedPersistentPropert @@ -73,7 +73,13 @@ class BasicRelationalPersistentProperty extends AnnotationBasedPersistentPropert
Assert.notNull(context, "context must not be null.");
this.context = context;
this.columnName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Column.class)).map(Column::value));
this.columnName = Lazy.of(() -> Optional.ofNullable( //
findAnnotation(Column.class)) //
.map(Column::value) //
.filter(StringUtils::hasText) //
);
this.keyColumnName = Lazy.of(() -> Optional.ofNullable( //
findAnnotation(Column.class)) //
.map(Column::keyColumn) //
@ -120,7 +126,7 @@ class BasicRelationalPersistentProperty extends AnnotationBasedPersistentPropert @@ -120,7 +126,7 @@ class BasicRelationalPersistentProperty extends AnnotationBasedPersistentPropert
@Override
public String getReverseColumnName() {
return context.getNamingStrategy().getReverseColumnName(this);
return columnName.get().orElseGet(() -> context.getNamingStrategy().getReverseColumnName(this));
}
@Override

2
src/main/java/org/springframework/data/relational/core/mapping/Column.java

@ -35,7 +35,7 @@ public @interface Column { @@ -35,7 +35,7 @@ public @interface Column {
/**
* The mapping column name.
*/
String value();
String value() default "";
/**
* The column name for key columns of List or Map collections.

2
src/test/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentPropertyUnitTests.java

@ -93,7 +93,7 @@ public class BasicRelationalPersistentPropertyUnitTests { @@ -93,7 +93,7 @@ public class BasicRelationalPersistentPropertyUnitTests {
.getRequiredPersistentEntity(DummyEntity.class) //
.getRequiredPersistentProperty("someList");
assertThat(listProperty.getColumnName()).isEqualTo("dummy_column_name");
assertThat(listProperty.getReverseColumnName()).isEqualTo("dummy_column_name");
assertThat(listProperty.getKeyColumn()).isEqualTo("dummy_key_column_name");
}

Loading…
Cancel
Save