42
42
import org .springframework .data .annotation .Id ;
43
43
import org .springframework .data .annotation .ReadOnlyProperty ;
44
44
import org .springframework .data .cassandra .core .StatementFactory ;
45
+ import org .springframework .data .cassandra .core .cql .PrimaryKeyType ;
45
46
import org .springframework .data .cassandra .core .cql .WriteOptions ;
46
47
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 .Table ;
53
- import org .springframework .data .cassandra .core .mapping .UserDefinedType ;
54
- import org .springframework .data .cassandra .core .mapping .UserTypeResolver ;
48
+ import org .springframework .data .cassandra .core .mapping .*;
55
49
import org .springframework .data .cassandra .support .UserDefinedTypeBuilder ;
56
50
import org .springframework .data .cassandra .test .util .RowMockUtil ;
57
51
@@ -127,10 +121,7 @@ void setUp() {
127
121
@ Test // DATACASS-172
128
122
void shouldWriteMappedUdt () {
129
123
130
- AddressUserType addressUserType = new AddressUserType ();
131
- addressUserType .setZip ("69469" );
132
- addressUserType .setCity ("Weinheim" );
133
- addressUserType .setStreetLines (Arrays .asList ("Heckenpfad" , "14" ));
124
+ AddressUserType addressUserType = prepareAddressUserType ();
134
125
135
126
AddressBook addressBook = new AddressBook ();
136
127
addressBook .setId ("1" );
@@ -146,10 +137,7 @@ void shouldWriteMappedUdt() {
146
137
@ Test // DATACASS-172
147
138
void shouldWriteMappedUdtCollection () {
148
139
149
- AddressUserType addressUserType = new AddressUserType ();
150
- addressUserType .setZip ("69469" );
151
- addressUserType .setCity ("Weinheim" );
152
- addressUserType .setStreetLines (Arrays .asList ("Heckenpfad" , "14" ));
140
+ AddressUserType addressUserType = prepareAddressUserType ();
153
141
154
142
AddressBook addressBook = new AddressBook ();
155
143
addressBook .setId ("1" );
@@ -188,10 +176,7 @@ void shouldWriteUdt() {
188
176
@ Test // DATACASS-172
189
177
void shouldWriteUdtPk () {
190
178
191
- AddressUserType addressUserType = new AddressUserType ();
192
- addressUserType .setZip ("69469" );
193
- addressUserType .setCity ("Weinheim" );
194
- addressUserType .setStreetLines (Arrays .asList ("Heckenpfad" , "14" ));
179
+ AddressUserType addressUserType = prepareAddressUserType ();
195
180
196
181
WithMappedUdtId withUdtId = new WithMappedUdtId ();
197
182
withUdtId .setId (addressUserType );
@@ -203,6 +188,72 @@ void shouldWriteUdtPk() {
203
188
"INSERT INTO withmappedudtid (id) " + "VALUES ({zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']})" );
204
189
}
205
190
191
+ @ Test // #1137
192
+ void shouldWriteCompositeUdtPk () {
193
+
194
+ AddressUserType addressUserType = prepareAddressUserType ();
195
+
196
+ WithCompositePrimaryKey withUdt = new WithCompositePrimaryKey ();
197
+ withUdt .addressUserType = addressUserType ;
198
+ withUdt .id = "foo" ;
199
+
200
+ SimpleStatement statement = new StatementFactory (converter ).insert (withUdt , WriteOptions .empty ())
201
+ .build (StatementBuilder .ParameterHandling .INLINE );
202
+
203
+ assertThat (statement .getQuery ()).isEqualTo ("INSERT INTO withcompositeprimarykey (id,addressusertype) "
204
+ + "VALUES ('foo',{zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']})" );
205
+ }
206
+
207
+ private static AddressUserType prepareAddressUserType () {
208
+
209
+ AddressUserType addressUserType = new AddressUserType ();
210
+ addressUserType .setZip ("69469" );
211
+ addressUserType .setCity ("Weinheim" );
212
+ addressUserType .setStreetLines (Arrays .asList ("Heckenpfad" , "14" ));
213
+
214
+ return addressUserType ;
215
+ }
216
+
217
+ @ Test // #1137
218
+ void shouldWriteCompositeUdtPkClass () {
219
+
220
+ WithCompositePrimaryKeyClassWithUdt object = prepareCompositePrimaryKeyClassWithUdt ();
221
+
222
+ SimpleStatement statement = new StatementFactory (converter ).insert (object , WriteOptions .empty ())
223
+ .build (StatementBuilder .ParameterHandling .INLINE );
224
+
225
+ assertThat (statement .getQuery ())
226
+ .isEqualTo ("INSERT INTO withcompositeprimarykeyclasswithudt (id,addressusertype,currency) "
227
+ + "VALUES ('foo',{zip:'69469',city:'Weinheim',streetlines:['Heckenpfad','14']},{currency:'EUR'})" );
228
+ }
229
+
230
+ @ Test // #1137
231
+ void shouldWriteCompositeUdtPkClassToWhere () {
232
+
233
+ WithCompositePrimaryKeyClassWithUdt object = prepareCompositePrimaryKeyClassWithUdt ();
234
+
235
+ Where where = new Where ();
236
+ converter .write (object , where );
237
+
238
+ assertThat ((UdtValue ) where .get (CqlIdentifier .fromCql ("currency" ))) //
239
+ .extracting (UdtValue ::getFormattedContents ) //
240
+ .isEqualTo ("{currency:'EUR'}" );
241
+ }
242
+
243
+ private static WithCompositePrimaryKeyClassWithUdt prepareCompositePrimaryKeyClassWithUdt () {
244
+
245
+ AddressUserType addressUserType = prepareAddressUserType ();
246
+
247
+ CompositePrimaryKeyClassWithUdt withUdt = new CompositePrimaryKeyClassWithUdt ();
248
+ withUdt .addressUserType = addressUserType ;
249
+ withUdt .id = "foo" ;
250
+ withUdt .currency = new Currency ("EUR" );
251
+
252
+ WithCompositePrimaryKeyClassWithUdt object = new WithCompositePrimaryKeyClassWithUdt ();
253
+ object .id = withUdt ;
254
+ return object ;
255
+ }
256
+
206
257
@ Test // DATACASS-172
207
258
void shouldWriteMappedUdtPk () {
208
259
@@ -585,6 +636,27 @@ public static class Money {
585
636
@ Id private Currency currency ;
586
637
}
587
638
639
+ @ Data
640
+ @ Table
641
+ public static class WithCompositePrimaryKey {
642
+ @ PrimaryKeyColumn (ordinal = 0 , type = PrimaryKeyType .PARTITIONED ) String id ;
643
+ @ PrimaryKeyColumn (ordinal = 1 ) AddressUserType addressUserType ;
644
+ }
645
+
646
+ @ Data
647
+ @ Table
648
+ public static class WithCompositePrimaryKeyClassWithUdt {
649
+ @ PrimaryKey CompositePrimaryKeyClassWithUdt id ;
650
+ }
651
+
652
+ @ Data
653
+ @ PrimaryKeyClass
654
+ public static class CompositePrimaryKeyClassWithUdt {
655
+ @ PrimaryKeyColumn (ordinal = 0 , type = PrimaryKeyType .PARTITIONED ) String id ;
656
+ @ PrimaryKeyColumn (ordinal = 1 ) AddressUserType addressUserType ;
657
+ @ PrimaryKeyColumn (ordinal = 2 ) Currency currency ;
658
+ }
659
+
588
660
@ Table
589
661
@ AllArgsConstructor
590
662
@ Getter
0 commit comments