DATACASS-93 - anno-based property name overrides now tested & working

This commit is contained in:
Matthew Adams
2014-02-20 15:24:35 -06:00
parent 7021419984
commit 22b05022a0
3 changed files with 60 additions and 17 deletions

View File

@@ -260,32 +260,52 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
List<CqlIdentifier> columnNames = new ArrayList<CqlIdentifier>();
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<CqlIdentifier> columnNames) {

View File

@@ -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;
}

View File

@@ -36,4 +36,17 @@ public class ForceQuotedEntitiesSimpleIntegrationTests {
@Table(value = EXPLICIT_TABLE_NAME, forceQuote = true)
public static class ExplicitTableNameForceQuoted {
}
@Test
public void testDefaultTableNameForceQuoted() {
BasicCassandraPersistentEntity<DefaultTableNameForceQuoted> entity = new BasicCassandraPersistentEntity<DefaultTableNameForceQuoted>(
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 {
}
}