Skip to content

Commit d01caa4

Browse files
committed
Refactor @PrimaryKey and @PrimaryKeyColumn to be aliases for @Column.
Both annotations are now aliases for `@Column`. Closes #1476
1 parent 04e8361 commit d01caa4

File tree

4 files changed

+20
-43
lines changed

4 files changed

+20
-43
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentProperty.java

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -166,33 +166,13 @@ private CqlIdentifier determineColumnName() {
166166
}
167167

168168
String overriddenName = null;
169-
170169
boolean forceQuote = false;
171170

172-
if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)
173-
174-
PrimaryKey primaryKey = findAnnotation(PrimaryKey.class);
175-
176-
if (primaryKey != null) {
177-
overriddenName = primaryKey.value();
178-
forceQuote = primaryKey.forceQuote();
179-
}
180-
} else if (isPrimaryKeyColumn()) { // then it's a simple type
181-
182-
PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
171+
Column column = findAnnotation(Column.class);
183172

184-
if (primaryKeyColumn != null) {
185-
overriddenName = primaryKeyColumn.value();
186-
forceQuote = primaryKeyColumn.forceQuote();
187-
}
188-
} else { // then it's a vanilla column with the assumption that it's mapped to a single column
189-
190-
Column column = findAnnotation(Column.class);
191-
192-
if (column != null) {
193-
overriddenName = column.value();
194-
forceQuote = column.forceQuote();
195-
}
173+
if (column != null) {
174+
overriddenName = column.value();
175+
forceQuote = column.forceQuote();
196176
}
197177

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

208-
if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)
209-
210-
PrimaryKey primaryKey = findAnnotation(PrimaryKey.class);
211-
212-
return primaryKey != null && !ObjectUtils.isEmpty(primaryKey.value());
213-
214-
} else if (isPrimaryKeyColumn()) { // then it's a simple type
215-
216-
PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
217-
218-
return primaryKeyColumn != null && !ObjectUtils.isEmpty(primaryKeyColumn.value());
219-
} else { // then it's a vanilla column with the assumption that it's mapped to a single column
220-
221-
Column column = findAnnotation(Column.class);
222-
223-
return column != null && !ObjectUtils.isEmpty(column.value());
224-
}
188+
Column column = findAnnotation(Column.class);
189+
return column != null && !ObjectUtils.isEmpty(column.value());
225190
}
226191

227192
@Override

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKey.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import org.springframework.core.annotation.AliasFor;
2425
import org.springframework.data.annotation.Id;
2526

2627
/**
@@ -43,11 +44,13 @@
4344
@Retention(value = RetentionPolicy.RUNTIME)
4445
@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD })
4546
@Id
47+
@Column
4648
public @interface PrimaryKey {
4749

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

5356
/**
@@ -58,5 +61,6 @@
5861
* @see com.datastax.oss.driver.api.core.CqlIdentifier#fromInternal(String)
5962
*/
6063
@Deprecated
64+
@AliasFor(annotation = Column.class, attribute = "forceQuote")
6165
boolean forceQuote() default false;
6266
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyColumn.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@
4040
@Documented
4141
@Retention(value = RetentionPolicy.RUNTIME)
4242
@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD })
43+
@Column
4344
public @interface PrimaryKeyColumn {
4445

4546
/**
4647
* The name of the column in the table.
4748
*/
48-
@AliasFor(attribute = "name")
49+
@AliasFor(annotation = Column.class, attribute = "value")
4950
String value() default "";
5051

5152
/**
5253
* The name of the column in the table.
5354
*/
54-
@AliasFor(attribute = "value")
55+
@AliasFor(annotation = Column.class, attribute = "value")
5556
String name() default "";
5657

5758
/**
@@ -78,5 +79,6 @@
7879
* @see com.datastax.oss.driver.api.core.CqlIdentifier#fromInternal(String)
7980
*/
8081
@Deprecated
82+
@AliasFor(annotation = Column.class, attribute = "forceQuote")
8183
boolean forceQuote() default false;
8284
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentPropertyUnitTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class BasicCassandraPersistentPropertyUnitTests {
4848
@Test
4949
void usesAnnotatedColumnName() {
5050
assertThat(getPropertyFor(Timeline.class, "text").getRequiredColumnName()).hasToString("message");
51+
assertThat(getPropertyFor(Timeline.class, "pkValue").getRequiredColumnName()).hasToString("val");
52+
assertThat(getPropertyFor(Timeline.class, "pkName").getRequiredColumnName()).hasToString("val");
5153
}
5254

5355
@Test
@@ -180,6 +182,10 @@ private static class Timeline {
180182
String keyspace;
181183

182184
@Column("table") String table;
185+
186+
@PrimaryKeyColumn(value = "val") String pkValue;
187+
188+
@PrimaryKeyColumn(name = "val") String pkName;
183189
}
184190

185191
@Retention(RetentionPolicy.RUNTIME)

0 commit comments

Comments
 (0)