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 ;
118
119
* @author Komi Innocent
119
120
* @author Christoph Strobl
120
121
* @author Mark Paluch
122
+ * @author Laszlo Csontos
121
123
*/
122
124
@ RunWith (SpringJUnit4ClassRunner .class )
123
125
@ ContextConfiguration ("classpath:infrastructure.xml" )
@@ -132,13 +134,19 @@ public class MongoTemplateTests {
132
134
133
135
@ Autowired MongoTemplate template ;
134
136
@ Autowired MongoDbFactory factory ;
135
- @ Autowired ConfigurableApplicationContext context ;
136
137
138
+ ConfigurableApplicationContext context ;
137
139
MongoTemplate mappingTemplate ;
138
140
org .springframework .data .util .Version mongoVersion ;
139
141
140
142
@ Rule public ExpectedException thrown = ExpectedException .none ();
141
143
144
+ @ Autowired
145
+ public void setApplicationContext (ConfigurableApplicationContext context ) {
146
+ context .addApplicationListener (new PersonWithIdPropertyOfTypeUUIDListener ());
147
+ this .context = context ;
148
+ }
149
+
142
150
@ Autowired
143
151
public void setMongo (Mongo mongo ) throws Exception {
144
152
@@ -150,7 +158,8 @@ public void setMongo(Mongo mongo) throws Exception {
150
158
PersonWith_idPropertyOfTypeString .class , PersonWithIdPropertyOfTypeObjectId .class ,
151
159
PersonWithIdPropertyOfTypeString .class , PersonWithIdPropertyOfTypeInteger .class ,
152
160
PersonWithIdPropertyOfTypeBigInteger .class , PersonWithIdPropertyOfPrimitiveInt .class ,
153
- PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class )));
161
+ PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class ,
162
+ PersonWithIdPropertyOfTypeUUID .class )));
154
163
mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
155
164
mappingContext .initialize ();
156
165
@@ -193,6 +202,7 @@ protected void cleanDb() {
193
202
template .dropCollection (PersonWithIdPropertyOfPrimitiveInt .class );
194
203
template .dropCollection (PersonWithIdPropertyOfTypeLong .class );
195
204
template .dropCollection (PersonWithIdPropertyOfPrimitiveLong .class );
205
+ template .dropCollection (PersonWithIdPropertyOfTypeUUID .class );
196
206
template .dropCollection (PersonWithVersionPropertyOfTypeInteger .class );
197
207
template .dropCollection (TestClass .class );
198
208
template .dropCollection (Sample .class );
@@ -627,6 +637,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
627
637
assertThat (p12q , notNullValue ());
628
638
assertThat (p12q .getId (), is (p12 .getId ()));
629
639
checkCollectionContents (PersonWithIdPropertyOfPrimitiveLong .class , 1 );
640
+
641
+ // DATAMONGO-1617
642
+ // UUID id - provided
643
+ PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID ();
644
+ p13 .setFirstName ("Sven_10" );
645
+ p13 .setAge (22 );
646
+ p13 .setId (UUID .randomUUID ());
647
+ // insert
648
+ mongoTemplate .insert (p13 );
649
+ // also try save
650
+ mongoTemplate .save (p13 );
651
+ assertThat (p13 .getId (), notNullValue ());
652
+ PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate .findOne (new Query (where ("id" ).in (p13 .getId ())),
653
+ PersonWithIdPropertyOfTypeUUID .class );
654
+ assertThat (p13q , notNullValue ());
655
+ assertThat (p13q .getId (), is (p13 .getId ()));
656
+ checkCollectionContents (PersonWithIdPropertyOfTypeUUID .class , 1 );
630
657
}
631
658
632
659
private void checkCollectionContents (Class <?> entityClass , int count ) {
@@ -1427,6 +1454,17 @@ public void doesNotFailOnVersionInitForUnversionedEntity() {
1427
1454
template .insert (document , template .determineCollectionName (PersonWithVersionPropertyOfTypeInteger .class ));
1428
1455
}
1429
1456
1457
+ @ Test // DATAMONGO-1617
1458
+ public void doesNotFailOnInsertForEntityWithNonAutogeneratableId () {
1459
+
1460
+ PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID ();
1461
+ person .setFirstName ("Laszlo" );
1462
+ person .setAge (33 );
1463
+
1464
+ template .insert (person );
1465
+ assertThat (person .getId (), is (notNullValue ()));
1466
+ }
1467
+
1430
1468
@ Test // DATAMONGO-539
1431
1469
public void removesObjectFromExplicitCollection () {
1432
1470
@@ -3570,4 +3608,19 @@ static class WithObjectTypeProperty {
3570
3608
@ Id String id ;
3571
3609
Object value ;
3572
3610
}
3611
+
3612
+ static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener <PersonWithIdPropertyOfTypeUUID > {
3613
+
3614
+ @ Override
3615
+ public void onBeforeConvert (BeforeConvertEvent <PersonWithIdPropertyOfTypeUUID > event ) {
3616
+ PersonWithIdPropertyOfTypeUUID person = event .getSource ();
3617
+
3618
+ if (person .getId () != null ) {
3619
+ return ;
3620
+ }
3621
+
3622
+ person .setId (UUID .randomUUID ());
3623
+ }
3624
+
3625
+ }
3573
3626
}
0 commit comments