Skip to content

Commit

Permalink
DATAJDBC-218 - Add support for key column in @column annotation.
Browse files Browse the repository at this point in the history
Original pull request: #83.
  • Loading branch information
florianluediger authored and schauder committed Sep 18, 2018
1 parent 1c15215 commit 0a86f3a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*
* @author Jens Schauder
* @author Greg Turnquist
* @author Florian Lüdiger
*/
class BasicRelationalPersistentProperty extends AnnotationBasedPersistentProperty<RelationalPersistentProperty>
implements RelationalPersistentProperty {
Expand All @@ -52,6 +53,7 @@ class BasicRelationalPersistentProperty extends AnnotationBasedPersistentPropert

private final RelationalMappingContext context;
private final Lazy<Optional<String>> columnName;
private final Lazy<Optional<String>> keyColumnName;

/**
* Creates a new {@link AnnotationBasedPersistentProperty}.
Expand All @@ -70,6 +72,8 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,

this.context = context;
this.columnName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Column.class)).map(Column::value));
this.keyColumnName = Lazy.of(() -> Optional.ofNullable(
findAnnotation(Column.class)).map(Column::keyColumn).filter(keyColumn -> !keyColumn.equals("")));
}

/*
Expand Down Expand Up @@ -116,7 +120,10 @@ public String getReverseColumnName() {

@Override
public String getKeyColumn() {
return isQualified() ? context.getNamingStrategy().getKeyColumn(this) : null;
if (isQualified())
return keyColumnName.get().orElseGet(() -> context.getNamingStrategy().getKeyColumn(this));
else
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* The annotation to configure the mapping from an attribute to a database column.
*
* @author Kazuki Shimizu
* @author Florian Lüdiger
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
Expand All @@ -35,4 +36,9 @@
* The mapping column name.
*/
String value();

/**
* The column name for key columns of List or Map collections.
*/
String keyColumn() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.assertj.core.api.SoftAssertions;
Expand All @@ -33,6 +34,7 @@
*
* @author Jens Schauder
* @author Oliver Gierke
* @author Florian Lüdiger
*/
public class BasicRelationalPersistentPropertyUnitTests {

Expand Down Expand Up @@ -84,6 +86,17 @@ public void detectsAnnotatedColumnName() {
.isEqualTo("dummy_last_updated_at");
}

@Test // DATAJDBC-218
public void detectsAnnotatedColumnAndKeyName() {

RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(DummyEntity.class);

assertThat(entity.getRequiredPersistentProperty("someList").getColumnName())
.isEqualTo("dummy_column_name");
assertThat(entity.getRequiredPersistentProperty("someList").getKeyColumn())
.isEqualTo("dummy_key_column_name");
}

private void checkTargetType(SoftAssertions softly, RelationalPersistentEntity<?> persistentEntity,
String propertyName, Class<?> expected) {

Expand All @@ -100,6 +113,9 @@ private static class DummyEntity {
private final ZonedDateTime zonedDateTime;
private final UUID uuid;

@Column(value="dummy_column_name", keyColumn="dummy_key_column_name")
private List<Integer> someList;

// DATACMNS-106
private @Column("dummy_name") String name;

Expand Down

0 comments on commit 0a86f3a

Please sign in to comment.