Refactor @PrimaryKey and @PrimaryKeyColumn to be aliases for @Column.

Both annotations are now aliases for `@Column`.

Closes #1476
This commit is contained in:
Mark Paluch
2024-02-08 09:31:09 +01:00
parent eb4402c8ab
commit fd80fcc610
4 changed files with 20 additions and 43 deletions

View File

@@ -166,33 +166,13 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
}
String overriddenName = null;
boolean forceQuote = false;
if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)
Column column = findAnnotation(Column.class);
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);
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);
@@ -205,23 +185,8 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
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

View File

@@ -21,6 +21,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.annotation.Id;
/**
@@ -43,11 +44,13 @@ import org.springframework.data.annotation.Id;
@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 "";
/**
@@ -58,5 +61,6 @@ public @interface PrimaryKey {
* @see com.datastax.oss.driver.api.core.CqlIdentifier#fromInternal(String)
*/
@Deprecated
@AliasFor(annotation = Column.class, attribute = "forceQuote")
boolean forceQuote() default false;
}

View File

@@ -40,18 +40,19 @@ import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
@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 "";
/**
@@ -78,5 +79,6 @@ public @interface PrimaryKeyColumn {
* @see com.datastax.oss.driver.api.core.CqlIdentifier#fromInternal(String)
*/
@Deprecated
@AliasFor(annotation = Column.class, attribute = "forceQuote")
boolean forceQuote() default false;
}

View File

@@ -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
@@ -180,6 +182,10 @@ class BasicCassandraPersistentPropertyUnitTests {
String keyspace;
@Column("table") String table;
@PrimaryKeyColumn(value = "val") String pkValue;
@PrimaryKeyColumn(name = "val") String pkName;
}
@Retention(RetentionPolicy.RUNTIME)