diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentProperty.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentProperty.java index 4c5f17d67..f65bf7e9f 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentProperty.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentProperty.java @@ -260,32 +260,52 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP List columnNames = new ArrayList(); - if (isCompositePrimaryKey()) { + if (isCompositePrimaryKey()) { // then the id type has @PrimaryKeyClass + addCompositePrimaryKeyColumnNames(getCompositePrimaryKeyEntity(), columnNames); return columnNames; } - // else not a composite primary key property -- first check @Column annotation - Column column = findAnnotation(Column.class); - if (column != null && StringUtils.hasText(column.value())) { - columnNames.add(cqlId(spelContext == null ? column.value() : SpelUtils.evaluate(column.value(), spelContext), - column.forceQuote())); - return columnNames; + // else we're dealing with a single-column field + String defaultName = getField().getName(); // TODO: replace with naming strategy class + 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 anno = findAnnotation(PrimaryKey.class); + overriddenName = anno == null ? null : anno.value(); + forceQuote = anno == null ? forceQuote : anno.forceQuote(); + + } else if (isPrimaryKeyColumn()) { // then it's a simple type + + PrimaryKeyColumn anno = findAnnotation(PrimaryKeyColumn.class); + overriddenName = anno == null ? null : anno.name(); + forceQuote = anno == null ? forceQuote : anno.forceQuote(); + + } else { // then it's a vanilla column with the assumption that it's mapped to a single column + + Column anno = findAnnotation(Column.class); + overriddenName = anno == null ? null : anno.value(); + forceQuote = anno == null ? forceQuote : anno.forceQuote(); + } - // else check @PrimaryKeyColumn annotation - PrimaryKeyColumn pk = findAnnotation(PrimaryKeyColumn.class); - if (pk != null && StringUtils.hasText(pk.name())) { - columnNames.add(cqlId(spelContext == null ? pk.name() : SpelUtils.evaluate(pk.name(), spelContext), - pk.forceQuote())); - return columnNames; - } - - // else default - columnNames.add(cqlId(field.getName())); // TODO: replace with naming strategy class + columnNames.add(createColumnName(defaultName, overriddenName, forceQuote)); return columnNames; } + protected CqlIdentifier createColumnName(String defaultName, String overriddenName, boolean forceQuote) { + + String name = defaultName; + + if (StringUtils.hasText(overriddenName)) { + name = spelContext == null ? overriddenName : SpelUtils.evaluate(overriddenName, spelContext); + } + + return cqlId(name, forceQuote); + } + protected void addCompositePrimaryKeyColumnNames(CassandraPersistentEntity compositePrimaryKeyEntity, final List columnNames) { diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/PrimaryKey.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/PrimaryKey.java index 61a8c88b4..2afd11014 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/PrimaryKey.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/PrimaryKey.java @@ -37,4 +37,14 @@ import org.springframework.data.annotation.Id; @Target(value = { ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE }) @Id public @interface PrimaryKey { + + /** + * The column name for the primary key if it is of a simple type, else ignored. + */ + String value() default ""; + + /** + * Whether to cause the column name to be force-quoted if the primary key is of a simple type, else ignored. + */ + boolean forceQuote() default false; } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/simple/ForceQuotedEntitiesSimpleIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/simple/ForceQuotedEntitiesSimpleIntegrationTests.java index 1966dd64b..edfd11404 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/simple/ForceQuotedEntitiesSimpleIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/simple/ForceQuotedEntitiesSimpleIntegrationTests.java @@ -36,4 +36,17 @@ public class ForceQuotedEntitiesSimpleIntegrationTests { @Table(value = EXPLICIT_TABLE_NAME, forceQuote = true) public static class ExplicitTableNameForceQuoted { } + + @Test + public void testDefaultTableNameForceQuoted() { + BasicCassandraPersistentEntity entity = new BasicCassandraPersistentEntity( + ClassTypeInformation.from(DefaultTableNameForceQuoted.class)); + + assertEquals(DefaultTableNameForceQuoted.class.getSimpleName().toLowerCase(), entity.getTableName().toCql()); + assertEquals(DefaultTableNameForceQuoted.class.getSimpleName().toLowerCase(), entity.getTableName().getUnquoted()); + } + + @Table + public static class DefaultTableNameForceQuoted { + } } \ No newline at end of file