From eef9066184dbeb406ef96c2c2fc46118e12346d3 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 3 Jan 2017 10:39:33 +0100 Subject: [PATCH] DATACASS-352 - Compare persistent properties using column names in CassandraPersistentPropertyComparator. We now compare persistent properties using their column names when both columns are regular columns. Column names either respect a defined column name or determine a column name based on the property name. Previously, the comparison extracted the column name itself and in case the other column was not annotated, the column compared with itself, the column name with its own property name which breaks the comparator contract. --- ...CassandraPersistentPropertyComparator.java | 56 +++++++++---------- ...PersistentPropertyComparatorUnitTests.java | 53 +++++++++++++----- 2 files changed, 66 insertions(+), 43 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentPropertyComparator.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentPropertyComparator.java index 2c8f9e553..e9cc2ad9d 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentPropertyComparator.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentPropertyComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors + * Copyright 2013-2017 the original author or authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,19 @@ package org.springframework.data.cassandra.mapping; import java.util.Comparator; +import org.springframework.cassandra.core.cql.CqlIdentifier; + /** * {@link Comparator} implementation that orders {@link CassandraPersistentProperty} instances. - *

- * Composite primary key properties and primary key properties sort before non-primary key properties. + *

+ * Composite primary key properties and primary key properties sort before non-primary key properties. Ordering rules: + *

* * @author Alex Shvid * @author Matthew T. Adams @@ -30,6 +39,16 @@ import java.util.Comparator; * @see org.springframework.data.cassandra.mapping.CassandraPersistentProperty */ public enum CassandraPersistentPropertyComparator implements Comparator { + + /** + * Comparator instance. + */ + INSTANCE, + + /** + * @deprecated as of 1.5, use {@link #INSTANCE} + */ + @Deprecated IT; @Override @@ -37,14 +56,11 @@ public enum CassandraPersistentPropertyComparator implements Comparator persistentEntity = context.getPersistentEntity(TwoColumns.class); + + CassandraPersistentProperty annotated = persistentEntity.getPersistentProperty("annotated"); + CassandraPersistentProperty another = persistentEntity.getPersistentProperty("anotherAnnotated"); + CassandraPersistentProperty plain = persistentEntity.getPersistentProperty("plain"); + + assertThat(CassandraPersistentPropertyComparator.IT.compare(annotated, plain), is(lessThanOrEqualTo(-1))); + assertThat(CassandraPersistentPropertyComparator.IT.compare(plain, annotated), is(greaterThanOrEqualTo(1))); + assertThat(CassandraPersistentPropertyComparator.IT.compare(another, another), is(0)); + } + + static class TwoColumns { + + @Column("annotated") String annotated; + + @Column("another") String anotherAnnotated; + + String plain; } }