82
82
import org .springframework .data .mongodb .core .mapping .Field ;
83
83
import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
84
84
import org .springframework .data .mongodb .core .mapping .event .AbstractMongoEventListener ;
85
+ import org .springframework .data .mongodb .core .mapping .event .BeforeConvertEvent ;
85
86
import org .springframework .data .mongodb .core .mapping .event .BeforeSaveEvent ;
86
87
import org .springframework .data .mongodb .core .query .BasicQuery ;
87
88
import org .springframework .data .mongodb .core .query .Criteria ;
119
120
* @author Komi Innocent
120
121
* @author Christoph Strobl
121
122
* @author Mark Paluch
123
+ * @author Laszlo Csontos
122
124
*/
123
125
@ RunWith (SpringJUnit4ClassRunner .class )
124
126
@ ContextConfiguration ("classpath:infrastructure.xml" )
@@ -134,13 +136,19 @@ public class MongoTemplateTests {
134
136
135
137
@ Autowired MongoTemplate template ;
136
138
@ Autowired MongoDbFactory factory ;
137
- @ Autowired ConfigurableApplicationContext context ;
138
139
140
+ ConfigurableApplicationContext context ;
139
141
MongoTemplate mappingTemplate ;
140
142
org .springframework .data .util .Version mongoVersion ;
141
143
142
144
@ Rule public ExpectedException thrown = ExpectedException .none ();
143
145
146
+ @ Autowired
147
+ public void setApplicationContext (ConfigurableApplicationContext context ) {
148
+ context .addApplicationListener (new PersonWithIdPropertyOfTypeUUIDListener ());
149
+ this .context = context ;
150
+ }
151
+
144
152
@ Autowired
145
153
public void setMongo (Mongo mongo ) throws Exception {
146
154
@@ -152,7 +160,8 @@ public void setMongo(Mongo mongo) throws Exception {
152
160
PersonWith_idPropertyOfTypeString .class , PersonWithIdPropertyOfTypeObjectId .class ,
153
161
PersonWithIdPropertyOfTypeString .class , PersonWithIdPropertyOfTypeInteger .class ,
154
162
PersonWithIdPropertyOfTypeBigInteger .class , PersonWithIdPropertyOfPrimitiveInt .class ,
155
- PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class )));
163
+ PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class ,
164
+ PersonWithIdPropertyOfTypeUUID .class )));
156
165
mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
157
166
mappingContext .initialize ();
158
167
@@ -195,6 +204,7 @@ protected void cleanDb() {
195
204
template .dropCollection (PersonWithIdPropertyOfPrimitiveInt .class );
196
205
template .dropCollection (PersonWithIdPropertyOfTypeLong .class );
197
206
template .dropCollection (PersonWithIdPropertyOfPrimitiveLong .class );
207
+ template .dropCollection (PersonWithIdPropertyOfTypeUUID .class );
198
208
template .dropCollection (PersonWithVersionPropertyOfTypeInteger .class );
199
209
template .dropCollection (TestClass .class );
200
210
template .dropCollection (Sample .class );
@@ -632,6 +642,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
632
642
assertThat (p12q , notNullValue ());
633
643
assertThat (p12q .getId (), is (p12 .getId ()));
634
644
checkCollectionContents (PersonWithIdPropertyOfPrimitiveLong .class , 1 );
645
+
646
+ // DATAMONGO-1617
647
+ // UUID id - provided
648
+ PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID ();
649
+ p13 .setFirstName ("Sven_10" );
650
+ p13 .setAge (22 );
651
+ p13 .setId (UUID .randomUUID ());
652
+ // insert
653
+ mongoTemplate .insert (p13 );
654
+ // also try save
655
+ mongoTemplate .save (p13 );
656
+ assertThat (p13 .getId (), notNullValue ());
657
+ PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate .findOne (new Query (where ("id" ).in (p13 .getId ())),
658
+ PersonWithIdPropertyOfTypeUUID .class );
659
+ assertThat (p13q , notNullValue ());
660
+ assertThat (p13q .getId (), is (p13 .getId ()));
661
+ checkCollectionContents (PersonWithIdPropertyOfTypeUUID .class , 1 );
635
662
}
636
663
637
664
private void checkCollectionContents (Class <?> entityClass , int count ) {
@@ -1429,6 +1456,17 @@ public void doesNotFailOnVersionInitForUnversionedEntity() {
1429
1456
template .insert (dbObject , template .determineCollectionName (PersonWithVersionPropertyOfTypeInteger .class ));
1430
1457
}
1431
1458
1459
+ @ Test // DATAMONGO-1617
1460
+ public void doesNotFailOnInsertForEntityWithNonAutogeneratableId () {
1461
+
1462
+ PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID ();
1463
+ person .setFirstName ("Laszlo" );
1464
+ person .setAge (33 );
1465
+
1466
+ template .insert (person );
1467
+ assertThat (person .getId (), is (notNullValue ()));
1468
+ }
1469
+
1432
1470
@ Test // DATAMONGO-539
1433
1471
public void removesObjectFromExplicitCollection () {
1434
1472
@@ -3545,4 +3583,19 @@ static class WithObjectTypeProperty {
3545
3583
@ Id String id ;
3546
3584
Object value ;
3547
3585
}
3586
+
3587
+ static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener <PersonWithIdPropertyOfTypeUUID > {
3588
+
3589
+ @ Override
3590
+ public void onBeforeConvert (BeforeConvertEvent <PersonWithIdPropertyOfTypeUUID > event ) {
3591
+ PersonWithIdPropertyOfTypeUUID person = event .getSource ();
3592
+
3593
+ if (person .getId () != null ) {
3594
+ return ;
3595
+ }
3596
+
3597
+ person .setId (UUID .randomUUID ());
3598
+ }
3599
+
3600
+ }
3548
3601
}
0 commit comments