77
77
import org .springframework .data .mongodb .core .mapping .Field ;
78
78
import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
79
79
import org .springframework .data .mongodb .core .mapping .event .AbstractMongoEventListener ;
80
+ import org .springframework .data .mongodb .core .mapping .event .BeforeConvertEvent ;
80
81
import org .springframework .data .mongodb .core .mapping .event .BeforeSaveEvent ;
81
82
import org .springframework .data .mongodb .core .query .BasicQuery ;
82
83
import org .springframework .data .mongodb .core .query .Criteria ;
113
114
* @author Thomas Darimont
114
115
* @author Komi Innocent
115
116
* @author Christoph Strobl
117
+ * @author Laszlo Csontos
116
118
*/
117
119
@ RunWith (SpringJUnit4ClassRunner .class )
118
120
@ ContextConfiguration ("classpath:infrastructure.xml" )
@@ -127,13 +129,19 @@ public class MongoTemplateTests {
127
129
128
130
@ Autowired MongoTemplate template ;
129
131
@ Autowired MongoDbFactory factory ;
130
- @ Autowired ConfigurableApplicationContext context ;
131
132
133
+ ConfigurableApplicationContext context ;
132
134
MongoTemplate mappingTemplate ;
133
135
org .springframework .data .util .Version mongoVersion ;
134
136
135
137
@ Rule public ExpectedException thrown = ExpectedException .none ();
136
138
139
+ @ Autowired
140
+ public void setApplicationContext (ConfigurableApplicationContext context ) {
141
+ context .addApplicationListener (new PersonWithIdPropertyOfTypeUUIDListener ());
142
+ this .context = context ;
143
+ }
144
+
137
145
@ Autowired
138
146
public void setMongo (Mongo mongo ) throws Exception {
139
147
@@ -145,7 +153,8 @@ public void setMongo(Mongo mongo) throws Exception {
145
153
PersonWith_idPropertyOfTypeString .class , PersonWithIdPropertyOfTypeObjectId .class ,
146
154
PersonWithIdPropertyOfTypeString .class , PersonWithIdPropertyOfTypeInteger .class ,
147
155
PersonWithIdPropertyOfTypeBigInteger .class , PersonWithIdPropertyOfPrimitiveInt .class ,
148
- PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class )));
156
+ PersonWithIdPropertyOfTypeLong .class , PersonWithIdPropertyOfPrimitiveLong .class ,
157
+ PersonWithIdPropertyOfTypeUUID .class )));
149
158
mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
150
159
mappingContext .initialize ();
151
160
@@ -188,6 +197,7 @@ protected void cleanDb() {
188
197
template .dropCollection (PersonWithIdPropertyOfPrimitiveInt .class );
189
198
template .dropCollection (PersonWithIdPropertyOfTypeLong .class );
190
199
template .dropCollection (PersonWithIdPropertyOfPrimitiveLong .class );
200
+ template .dropCollection (PersonWithIdPropertyOfTypeUUID .class );
191
201
template .dropCollection (PersonWithVersionPropertyOfTypeInteger .class );
192
202
template .dropCollection (TestClass .class );
193
203
template .dropCollection (Sample .class );
@@ -643,6 +653,23 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
643
653
assertThat (p12q , notNullValue ());
644
654
assertThat (p12q .getId (), is (p12 .getId ()));
645
655
checkCollectionContents (PersonWithIdPropertyOfPrimitiveLong .class , 1 );
656
+
657
+ // DATAMONGO-1617
658
+ // UUID id - provided
659
+ PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID ();
660
+ p13 .setFirstName ("Sven_10" );
661
+ p13 .setAge (22 );
662
+ p13 .setId (UUID .randomUUID ());
663
+ // insert
664
+ mongoTemplate .insert (p13 );
665
+ // also try save
666
+ mongoTemplate .save (p13 );
667
+ assertThat (p13 .getId (), notNullValue ());
668
+ PersonWithIdPropertyOfTypeUUID p13q = mongoTemplate .findOne (new Query (where ("id" ).in (p13 .getId ())),
669
+ PersonWithIdPropertyOfTypeUUID .class );
670
+ assertThat (p13q , notNullValue ());
671
+ assertThat (p13q .getId (), is (p13 .getId ()));
672
+ checkCollectionContents (PersonWithIdPropertyOfTypeUUID .class , 1 );
646
673
}
647
674
648
675
private void checkCollectionContents (Class <?> entityClass , int count ) {
@@ -1497,6 +1524,17 @@ public void doesNotFailOnVersionInitForUnversionedEntity() {
1497
1524
template .insert (dbObject , template .determineCollectionName (PersonWithVersionPropertyOfTypeInteger .class ));
1498
1525
}
1499
1526
1527
+ @ Test // DATAMONGO-1617
1528
+ public void doesNotFailOnInsertForEntityWithNonAutogeneratableId () {
1529
+
1530
+ PersonWithIdPropertyOfTypeUUID person = new PersonWithIdPropertyOfTypeUUID ();
1531
+ person .setFirstName ("Laszlo" );
1532
+ person .setAge (33 );
1533
+
1534
+ template .insert (person );
1535
+ assertThat (person .getId (), is (notNullValue ()));
1536
+ }
1537
+
1500
1538
/**
1501
1539
* @see DATAMONGO-539
1502
1540
*/
@@ -3590,4 +3628,19 @@ static class WithObjectTypeProperty {
3590
3628
@ Id String id ;
3591
3629
Object value ;
3592
3630
}
3631
+
3632
+ static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener <PersonWithIdPropertyOfTypeUUID > {
3633
+
3634
+ @ Override
3635
+ public void onBeforeConvert (BeforeConvertEvent <PersonWithIdPropertyOfTypeUUID > event ) {
3636
+ PersonWithIdPropertyOfTypeUUID person = event .getSource ();
3637
+
3638
+ if (person .getId () != null ) {
3639
+ return ;
3640
+ }
3641
+
3642
+ person .setId (UUID .randomUUID ());
3643
+ }
3644
+
3645
+ }
3593
3646
}
0 commit comments