Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAJDBC-218 - Add support for key column in @Column annotation #83

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,7 @@ 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));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default value for the keyColumn field is "", wouldn't you want to filter that out? Perhaps something like:

...map(Column::keyColumn).filter(x -> x != null && !x.equals(""))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right, filtering out the default value is important here, I have added that.

Why are you also checking x for being not null, that case can not come up if I'm not mistaken.

Copy link

@kedev kedev Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's my bad. Forgot that null isn't an allowable value for annotations. It's been a while since I've written a custom annotation processor :)

}

/*
Expand Down Expand Up @@ -116,7 +119,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