Skip to content

Commit b793c94

Browse files
committed
Do not sort properties by their column name.
We now no longer sort properties by their column name. Instead, we consider only primary keys, ordinals and tuple element ordinals in the order of: Partitioning primary keys (can be ordered with ordinals), partition primary keys (can be ordered with ordinals), all other columns. Tuples: Elements by their ordinal. Closes #1369
1 parent 800b6b9 commit b793c94

13 files changed

+173
-132
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ public enum CassandraPersistentPropertyComparator implements Comparator<Cassandr
4545
@Override
4646
public int compare(CassandraPersistentProperty left, CassandraPersistentProperty right) {
4747

48-
if (left == null && right == null) {
49-
return 0;
50-
} else if (left != null && right == null) {
51-
return 1;
52-
} else if (left == null) {
53-
return -1;
54-
} else if (left.equals(right)) {
55-
return 0;
56-
}
57-
5848
boolean leftIsCompositePrimaryKey = left.isCompositePrimaryKey();
5949
boolean rightIsCompositePrimaryKey = right.isCompositePrimaryKey();
6050

@@ -86,7 +76,15 @@ public int compare(CassandraPersistentProperty left, CassandraPersistentProperty
8676
return 1;
8777
}
8878

89-
// else, neither property is a composite primary key nor a primary key; compare @Column annotations
90-
return left.getRequiredColumnName().toString().compareTo(right.getRequiredColumnName().toString());
79+
Element leftAnnotation = left.findAnnotation(Element.class);
80+
Element rightAnnotation = right.findAnnotation(Element.class);
81+
82+
if (leftAnnotation != null && rightAnnotation != null) {
83+
return Integer.compare(leftAnnotation.value(), rightAnnotation.value());
84+
}
85+
86+
// else, neither property is a composite primary key nor a primary key; there is nothing more so from that
87+
// perspective, columns are equal.
88+
return 0;
9189
}
9290
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public int compare(PrimaryKeyColumn left, PrimaryKeyColumn right) {
4444
int comparison = left.type().compareTo(right.type());
4545

4646
comparison = (comparison != 0 ? comparison : Integer.compare(left.ordinal(), right.ordinal()));
47-
comparison = (comparison != 0 ? comparison : left.name().compareTo(right.name()));
48-
comparison = (comparison != 0 ? comparison : left.ordering().compareTo(right.ordering()));
4947

5048
return comparison;
5149
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ void insertShouldInsertEntity() {
319319
assertThat(getUninterruptibly(future)).isEqualTo(user);
320320
verify(session).executeAsync(statementCaptor.capture());
321321
assertThat(render(statementCaptor.getValue()))
322-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES ('Walter','heisenberg','White')");
322+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('heisenberg','Walter','White')");
323323
assertThat(beforeConvert).isSameAs(user);
324324
assertThat(beforeSave).isSameAs(user);
325325
}
@@ -341,7 +341,7 @@ void insertShouldConsiderEntityAfterCallback() {
341341
assertThat(getUninterruptibly(future)).isNotSameAs(user);
342342
verify(session).executeAsync(statementCaptor.capture());
343343
assertThat(render(statementCaptor.getValue()))
344-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES ('Walter','ww','White')");
344+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('ww','Walter','White')");
345345
}
346346

347347
@Test
@@ -357,7 +357,7 @@ void insertShouldInsertVersionedEntity() {
357357
assertThat(getUninterruptibly(future)).isEqualTo(user);
358358
verify(session).executeAsync(statementCaptor.capture());
359359
assertThat(render(statementCaptor.getValue())).isEqualTo(
360-
"INSERT INTO vusers (firstname,id,lastname,version) VALUES ('Walter','heisenberg','White',0) IF NOT EXISTS");
360+
"INSERT INTO vusers (id,version,firstname,lastname) VALUES ('heisenberg',0,'Walter','White') IF NOT EXISTS");
361361
assertThat(beforeConvert).isSameAs(user);
362362
assertThat(beforeSave).isSameAs(user);
363363
}
@@ -412,7 +412,7 @@ void updateShouldUpdateVersionedEntity() {
412412
verify(session).executeAsync(statementCaptor.capture());
413413
SimpleStatement value = statementCaptor.getValue();
414414
assertThat(render(value)).isEqualTo(
415-
"UPDATE vusers SET firstname='Walter', lastname='White', version=1 WHERE id='heisenberg' IF version=0");
415+
"UPDATE vusers SET version=1, firstname='Walter', lastname='White' WHERE id='heisenberg' IF version=0");
416416
assertThat(beforeConvert).isSameAs(user);
417417
assertThat(beforeSave).isSameAs(user);
418418
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.mockito.ArgumentMatchers.*;
2020
import static org.mockito.Mockito.*;
2121

22+
import java.io.Serializable;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collection;
@@ -36,10 +37,15 @@
3637
import org.mockito.quality.Strictness;
3738
import org.springframework.data.cassandra.core.convert.SchemaFactory;
3839
import org.springframework.data.cassandra.core.cql.CqlOperations;
40+
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
3941
import org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification;
4042
import org.springframework.data.cassandra.core.cql.keyspace.UserTypeNameSpecification;
4143
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
4244
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
45+
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
46+
import org.springframework.data.cassandra.core.mapping.PrimaryKeyClass;
47+
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
48+
import org.springframework.data.cassandra.core.mapping.Table;
4349
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
4450
import org.springframework.data.convert.CustomConversions;
4551

@@ -182,6 +188,19 @@ void createsIndexes() {
182188
verify(operations).execute("CREATE INDEX ON indexedentity (firstname);");
183189
}
184190

191+
@Test // DATACASS-213
192+
void foo() {
193+
194+
context.getPersistentEntity(Person.class);
195+
196+
CassandraPersistentEntitySchemaCreator schemaCreator = new CassandraPersistentEntitySchemaCreator(context,
197+
adminOperations);
198+
199+
schemaCreator.createTables(false);
200+
201+
// verify(operations).execute("CREATE INDEX ON indexedentity (firstname);");
202+
}
203+
185204
private void verifyTypesGetCreatedInOrderFor(String... typenames) {
186205

187206
ArgumentCaptor<String> cql = ArgumentCaptor.forClass(String.class);
@@ -216,4 +235,22 @@ private static class Udt2 extends AbstractModel {
216235

217236
private Udt1 u1;
218237
}
238+
239+
@PrimaryKeyClass
240+
public static class PersonKey implements Serializable {
241+
@PrimaryKeyColumn(name = "firstname", type = PrimaryKeyType.PARTITIONED) private String firstName;
242+
243+
@PrimaryKeyColumn(name = "aname", type = PrimaryKeyType.PARTITIONED) private String aName;
244+
245+
@PrimaryKeyColumn(name = "lastname", type = PrimaryKeyType.CLUSTERED) private String lastName;
246+
247+
@PrimaryKeyColumn(name = "bname", type = PrimaryKeyType.CLUSTERED) private String bName;
248+
}
249+
250+
@Table
251+
public static class Person {
252+
@PrimaryKey PersonKey key;
253+
254+
int age;
255+
}
219256
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void insertShouldInsertEntity() {
301301

302302
verify(session).execute(statementCaptor.capture());
303303
assertThat(render(statementCaptor.getValue()))
304-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES ('Walter','heisenberg','White')");
304+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('heisenberg','Walter','White')");
305305
assertThat(beforeConvert).isSameAs(user);
306306
assertThat(beforeSave).isSameAs(user);
307307
}
@@ -322,7 +322,7 @@ void insertShouldConsiderEntityAfterCallback() {
322322

323323
verify(session).execute(statementCaptor.capture());
324324
assertThat(render(statementCaptor.getValue()))
325-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES ('Walter','ww','White')");
325+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('ww','Walter','White')");
326326
}
327327

328328
@Test
@@ -337,7 +337,7 @@ void insertShouldInsertVersionedEntity() {
337337

338338
verify(session).execute(statementCaptor.capture());
339339
assertThat(render(statementCaptor.getValue())).isEqualTo(
340-
"INSERT INTO vusers (firstname,id,lastname,version) VALUES ('Walter','heisenberg','White',0) IF NOT EXISTS");
340+
"INSERT INTO vusers (id,version,firstname,lastname) VALUES ('heisenberg',0,'Walter','White') IF NOT EXISTS");
341341
assertThat(beforeConvert).isSameAs(user);
342342
assertThat(beforeSave).isSameAs(user);
343343
}
@@ -358,7 +358,7 @@ void insertShouldInsertVersionedEntityAfterCallback() {
358358

359359
verify(session).execute(statementCaptor.capture());
360360
assertThat(render(statementCaptor.getValue())).isEqualTo(
361-
"INSERT INTO vusers (firstname,id,lastname,version) VALUES ('Walter','ww','White',0) IF NOT EXISTS");
361+
"INSERT INTO vusers (id,version,firstname,lastname) VALUES ('ww',0,'Walter','White') IF NOT EXISTS");
362362
}
363363

364364
@Test
@@ -375,7 +375,7 @@ void insertShouldInsertWithOptionsEntity() {
375375

376376
verify(session).execute(statementCaptor.capture());
377377
assertThat(render(statementCaptor.getValue()))
378-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES ('Walter','heisenberg','White') IF NOT EXISTS");
378+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('heisenberg','Walter','White') IF NOT EXISTS");
379379
}
380380

381381
@Test // DATACASS-560
@@ -391,7 +391,7 @@ void insertShouldInsertWithNulls() {
391391

392392
verify(session).execute(statementCaptor.capture());
393393
assertThat(render(statementCaptor.getValue()))
394-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES (NULL,'heisenberg',NULL)");
394+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('heisenberg',NULL,NULL)");
395395
}
396396

397397
@Test // DATACASS-292
@@ -449,7 +449,7 @@ void updateShouldUpdateVersionedEntity() {
449449

450450
verify(session).execute(statementCaptor.capture());
451451
assertThat(render(statementCaptor.getValue())).isEqualTo(
452-
"UPDATE vusers SET firstname='Walter', lastname='White', version=1 WHERE id='heisenberg' IF version=0");
452+
"UPDATE vusers SET version=1, firstname='Walter', lastname='White' WHERE id='heisenberg' IF version=0");
453453
assertThat(beforeConvert).isSameAs(user);
454454
assertThat(beforeSave).isSameAs(user);
455455
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void insertShouldInsertEntity() {
291291

292292
verify(session).execute(statementCaptor.capture());
293293
assertThat(render(statementCaptor.getValue()))
294-
.isEqualTo("INSERT INTO users (firstname,id,lastname) VALUES ('Walter','heisenberg','White')");
294+
.isEqualTo("INSERT INTO users (id,firstname,lastname) VALUES ('heisenberg','Walter','White')");
295295
assertThat(beforeConvert).isSameAs(user);
296296
assertThat(beforeSave).isSameAs(user);
297297
}
@@ -307,7 +307,7 @@ void insertShouldInsertVersionedEntity() {
307307

308308
verify(session).execute(statementCaptor.capture());
309309
assertThat(render(statementCaptor.getValue())).isEqualTo(
310-
"INSERT INTO vusers (firstname,id,lastname,version) VALUES ('Walter','heisenberg','White',0) IF NOT EXISTS");
310+
"INSERT INTO vusers (id,version,firstname,lastname) VALUES ('heisenberg',0,'Walter','White') IF NOT EXISTS");
311311
assertThat(beforeConvert).isSameAs(user);
312312
assertThat(beforeSave).isSameAs(user);
313313
}
@@ -355,7 +355,7 @@ void updateShouldUpdateVersionedEntity() {
355355

356356
verify(session).execute(statementCaptor.capture());
357357
assertThat(render(statementCaptor.getValue())).isEqualTo(
358-
"UPDATE vusers SET firstname='Walter', lastname='White', version=1 WHERE id='heisenberg' IF version=0");
358+
"UPDATE vusers SET version=1, firstname='Walter', lastname='White' WHERE id='heisenberg' IF version=0");
359359
assertThat(beforeConvert).isSameAs(user);
360360
assertThat(beforeSave).isSameAs(user);
361361
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void shouldCreateSetInsertNulls() {
317317
StatementBuilder<RegularInsert> insert = statementFactory.insert(person, options);
318318

319319
assertThat(insert.build(ParameterHandling.INLINE).getQuery()).isEqualTo(
320-
"INSERT INTO person (first_name,id,list,map,number,set_col) VALUES (NULL,'foo',NULL,NULL,NULL,NULL)");
320+
"INSERT INTO person (id,number,list,map,set_col,first_name) VALUES ('foo',NULL,NULL,NULL,NULL,NULL)");
321321
}
322322

323323
@Test // DATACASS-656
@@ -596,7 +596,7 @@ void shouldCreateSetUpdateFromObject() {
596596
WriteOptions.empty());
597597

598598
assertThat(update.build(ParameterHandling.INLINE).getQuery())
599-
.isEqualTo("UPDATE person SET first_name='bar', list=NULL, map=NULL, number=NULL, set_col=NULL WHERE id='foo'");
599+
.isEqualTo("UPDATE person SET number=NULL, list=NULL, map=NULL, set_col=NULL, first_name='bar' WHERE id='foo'");
600600
}
601601

602602
@Test // DATACASS-656
@@ -665,7 +665,7 @@ void shouldCreateSetUpdateFromObjectWithEmptyCollections() {
665665
WriteOptions.empty());
666666

667667
assertThat(update.build(ParameterHandling.INLINE).getQuery())
668-
.isEqualTo("UPDATE person SET first_name=NULL, list=[], map=NULL, number=NULL, set_col={} WHERE id='foo'");
668+
.isEqualTo("UPDATE person SET number=NULL, list=[], map=NULL, set_col={}, first_name=NULL WHERE id='foo'");
669669
}
670670

671671
@Test // DATACASS-708

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverterUDTUnitTests.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,7 @@
4444
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
4545
import org.springframework.data.cassandra.core.cql.WriteOptions;
4646
import org.springframework.data.cassandra.core.cql.util.StatementBuilder;
47-
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
48-
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
49-
import org.springframework.data.cassandra.core.mapping.CassandraType;
50-
import org.springframework.data.cassandra.core.mapping.Embedded;
51-
import org.springframework.data.cassandra.core.mapping.Frozen;
52-
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
53-
import org.springframework.data.cassandra.core.mapping.PrimaryKeyClass;
54-
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
55-
import org.springframework.data.cassandra.core.mapping.Table;
56-
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
57-
import org.springframework.data.cassandra.core.mapping.UserTypeResolver;
47+
import org.springframework.data.cassandra.core.mapping.*;
5848
import org.springframework.data.cassandra.support.UserDefinedTypeBuilder;
5949
import org.springframework.data.cassandra.test.util.RowMockUtil;
6050

@@ -139,8 +129,8 @@ void shouldWriteMappedUdt() {
139129
SimpleStatement statement = new StatementFactory(converter).insert(addressBook, WriteOptions.empty())
140130
.build(StatementBuilder.ParameterHandling.INLINE);
141131

142-
assertThat(statement.getQuery()).isEqualTo("INSERT INTO addressbook (currentaddress,id) "
143-
+ "VALUES ({zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']},'1')");
132+
assertThat(statement.getQuery()).isEqualTo("INSERT INTO addressbook (id,currentaddress) "
133+
+ "VALUES ('1',{zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']})");
144134
}
145135

146136
@Test // DATACASS-172
@@ -178,8 +168,8 @@ void shouldWriteUdt() {
178168
SimpleStatement statement = new StatementFactory(converter).insert(addressBook, WriteOptions.empty())
179169
.build(StatementBuilder.ParameterHandling.INLINE);
180170

181-
assertThat(statement.getQuery()).isEqualTo("INSERT INTO addressbook (alternate,id) "
182-
+ "VALUES ({zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']},'1')");
171+
assertThat(statement.getQuery()).isEqualTo("INSERT INTO addressbook (id,alternate) "
172+
+ "VALUES ('1',{zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']})");
183173
}
184174

185175
@Test // DATACASS-172
@@ -484,7 +474,7 @@ void shouldWriteNestedUdt() {
484474
.build(StatementBuilder.ParameterHandling.INLINE);
485475

486476
assertThat(statement.getQuery())
487-
.isEqualTo("INSERT INTO car (engine,id) VALUES ({manufacturer:{name:'a good one',displayname:NULL}},'1')");
477+
.isEqualTo("INSERT INTO car (id,engine) VALUES ('1',{manufacturer:{name:'a good one',displayname:NULL}})");
488478
}
489479

490480
@Test // #1098

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/convert/SchemaFactoryUnitTests.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,7 @@
4343
import org.springframework.data.cassandra.core.cql.keyspace.CreateIndexSpecification;
4444
import org.springframework.data.cassandra.core.cql.keyspace.CreateIndexSpecification.ColumnFunction;
4545
import org.springframework.data.cassandra.core.cql.keyspace.CreateTableSpecification;
46-
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
47-
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
48-
import org.springframework.data.cassandra.core.mapping.CassandraType;
49-
import org.springframework.data.cassandra.core.mapping.Column;
50-
import org.springframework.data.cassandra.core.mapping.Element;
51-
import org.springframework.data.cassandra.core.mapping.Embedded;
52-
import org.springframework.data.cassandra.core.mapping.Indexed;
53-
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
54-
import org.springframework.data.cassandra.core.mapping.PrimaryKeyClass;
55-
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
56-
import org.springframework.data.cassandra.core.mapping.Table;
57-
import org.springframework.data.cassandra.core.mapping.Tuple;
46+
import org.springframework.data.cassandra.core.mapping.*;
5847
import org.springframework.data.cassandra.domain.AllPossibleTypes;
5948
import org.springframework.data.cassandra.support.UserDefinedTypeBuilder;
6049
import org.springframework.data.mapping.MappingException;
@@ -910,4 +899,32 @@ void createdTableSpecificationShouldConsiderStaticForTypedTupleColumns() {
910899
assertThat(name.getName().toString()).isEqualTo("address");
911900
assertThat(name.isStatic()).isTrue();
912901
}
902+
903+
@Test // GH-978
904+
void aaa() {
905+
906+
CassandraPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(Person.class);
907+
908+
CreateTableSpecification tableSpecification = schemaFactory.getCreateTableSpecificationFor(persistentEntity);
909+
910+
System.out.println(tableSpecification);
911+
}
912+
913+
@PrimaryKeyClass
914+
public static class PersonKey implements Serializable {
915+
@PrimaryKeyColumn(name = "firstname", type = PrimaryKeyType.PARTITIONED, ordinal = 1) private String firstName;
916+
917+
@PrimaryKeyColumn(name = "aname", type = PrimaryKeyType.PARTITIONED, ordinal = 0) private String aName;
918+
919+
@PrimaryKeyColumn(name = "lastname", type = PrimaryKeyType.CLUSTERED, ordinal = 3) private String lastName;
920+
921+
@PrimaryKeyColumn(name = "bname", type = PrimaryKeyType.CLUSTERED, ordinal = 4) private String bName;
922+
}
923+
924+
@Table
925+
public static class Person {
926+
@PrimaryKey PersonKey key;
927+
928+
int age;
929+
}
913930
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Mark Paluch
3434
*/
35-
class CachedPreparedStatementCreatorIntegrationTest extends AbstractKeyspaceCreatingIntegrationTests {
35+
class CachedPreparedStatementCreatorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTests {
3636

3737
private static final AtomicBoolean initialized = new AtomicBoolean();
3838

@@ -59,6 +59,6 @@ void shouldRetainIdempotencyFlag() {
5959
PreparedStatement preparedStatement = CachedPreparedStatementCreator.of(cache, insert)
6060
.createPreparedStatement(session);
6161

62-
assertThat(preparedStatement.bind(1, 2).isIdempotent()).isTrue();
62+
assertThat(preparedStatement.bind("id", "foo").isIdempotent()).isTrue();
6363
}
6464
}

0 commit comments

Comments
 (0)