Skip to content

Commit

Permalink
Refactor @PrimaryKey and @PrimaryKeyColumn to be aliases for `@Co…
Browse files Browse the repository at this point in the history
…lumn`.

Both annotations are now aliases for `@Column`.

Closes #1476
  • Loading branch information
mp911de committed Feb 8, 2024
1 parent 04e8361 commit d01caa4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,33 +166,13 @@ private CqlIdentifier determineColumnName() {
}

String overriddenName = null;

boolean forceQuote = false;

if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)

PrimaryKey primaryKey = findAnnotation(PrimaryKey.class);

if (primaryKey != null) {
overriddenName = primaryKey.value();
forceQuote = primaryKey.forceQuote();
}
} else if (isPrimaryKeyColumn()) { // then it's a simple type

PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
Column column = findAnnotation(Column.class);

if (primaryKeyColumn != null) {
overriddenName = primaryKeyColumn.value();
forceQuote = primaryKeyColumn.forceQuote();
}
} else { // then it's a vanilla column with the assumption that it's mapped to a single column

Column column = findAnnotation(Column.class);

if (column != null) {
overriddenName = column.value();
forceQuote = column.forceQuote();
}
if (column != null) {
overriddenName = column.value();
forceQuote = column.forceQuote();
}

return namingAccessor.generate(overriddenName, forceQuote, NamingStrategy::getColumnName, this, this.spelContext);
Expand All @@ -205,23 +185,8 @@ public boolean hasExplicitColumnName() {
return false;
}

if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)

PrimaryKey primaryKey = findAnnotation(PrimaryKey.class);

return primaryKey != null && !ObjectUtils.isEmpty(primaryKey.value());

} else if (isPrimaryKeyColumn()) { // then it's a simple type

PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);

return primaryKeyColumn != null && !ObjectUtils.isEmpty(primaryKeyColumn.value());
} else { // then it's a vanilla column with the assumption that it's mapped to a single column

Column column = findAnnotation(Column.class);

return column != null && !ObjectUtils.isEmpty(column.value());
}
Column column = findAnnotation(Column.class);
return column != null && !ObjectUtils.isEmpty(column.value());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;
import org.springframework.data.annotation.Id;

/**
Expand All @@ -43,11 +44,13 @@
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD })
@Id
@Column
public @interface PrimaryKey {

/**
* The column name for the primary key if it is of a simple type, else ignored.
*/
@AliasFor(annotation = Column.class, attribute = "value")
String value() default "";

/**
Expand All @@ -58,5 +61,6 @@
* @see com.datastax.oss.driver.api.core.CqlIdentifier#fromInternal(String)
*/
@Deprecated
@AliasFor(annotation = Column.class, attribute = "forceQuote")
boolean forceQuote() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@
@Documented
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD })
@Column
public @interface PrimaryKeyColumn {

/**
* The name of the column in the table.
*/
@AliasFor(attribute = "name")
@AliasFor(annotation = Column.class, attribute = "value")
String value() default "";

/**
* The name of the column in the table.
*/
@AliasFor(attribute = "value")
@AliasFor(annotation = Column.class, attribute = "value")
String name() default "";

/**
Expand All @@ -78,5 +79,6 @@
* @see com.datastax.oss.driver.api.core.CqlIdentifier#fromInternal(String)
*/
@Deprecated
@AliasFor(annotation = Column.class, attribute = "forceQuote")
boolean forceQuote() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class BasicCassandraPersistentPropertyUnitTests {
@Test
void usesAnnotatedColumnName() {
assertThat(getPropertyFor(Timeline.class, "text").getRequiredColumnName()).hasToString("message");
assertThat(getPropertyFor(Timeline.class, "pkValue").getRequiredColumnName()).hasToString("val");
assertThat(getPropertyFor(Timeline.class, "pkName").getRequiredColumnName()).hasToString("val");
}

@Test
Expand Down Expand Up @@ -180,6 +182,10 @@ private static class Timeline {
String keyspace;

@Column("table") String table;

@PrimaryKeyColumn(value = "val") String pkValue;

@PrimaryKeyColumn(name = "val") String pkName;
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down

0 comments on commit d01caa4

Please sign in to comment.