From d01caa4795a0392c1b19af619d9ba44520e50bd6 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 8 Feb 2024 09:31:09 +0100 Subject: [PATCH] Refactor `@PrimaryKey` and `@PrimaryKeyColumn` to be aliases for `@Column`. Both annotations are now aliases for `@Column`. Closes #1476 --- .../BasicCassandraPersistentProperty.java | 47 +++---------------- .../cassandra/core/mapping/PrimaryKey.java | 4 ++ .../core/mapping/PrimaryKeyColumn.java | 6 ++- ...cCassandraPersistentPropertyUnitTests.java | 6 +++ 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentProperty.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentProperty.java index 9836e3275..c44e55790 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentProperty.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentProperty.java @@ -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); @@ -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 diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKey.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKey.java index fc248dc05..835d01043 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKey.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKey.java @@ -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; /** @@ -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 ""; /** @@ -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; } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyColumn.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyColumn.java index 577cde8b4..31eaf3e16 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyColumn.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyColumn.java @@ -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 ""; /** @@ -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; } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentPropertyUnitTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentPropertyUnitTests.java index 9f1aa7289..28344568e 100755 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentPropertyUnitTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentPropertyUnitTests.java @@ -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 @@ private static class Timeline { String keyspace; @Column("table") String table; + + @PrimaryKeyColumn(value = "val") String pkValue; + + @PrimaryKeyColumn(name = "val") String pkName; } @Retention(RetentionPolicy.RUNTIME)