@@ -1180,6 +1180,73 @@ void allowsOneToMayStyleLookupsUsingSelfVariable() {
1180
1180
assertThat (target .books ).containsExactlyInAnyOrder (book1 , book3 );
1181
1181
}
1182
1182
1183
+ @ Test // GH-3847
1184
+ void writeReferenceWithIdStringThatIsAnObjectId () {
1185
+
1186
+ String rootCollectionName = template .getCollectionName (SingleRefRoot .class );
1187
+
1188
+ ObjectId id = new ObjectId ();
1189
+
1190
+ SingleRefRoot source = new SingleRefRoot ();
1191
+ source .id = "root-1" ;
1192
+ source .simpleValueRef = new SimpleObjectRef (id .toHexString (), "me-the-referenced-object" );
1193
+
1194
+ template .save (source );
1195
+
1196
+ Document target = template .execute (db -> {
1197
+ return db .getCollection (rootCollectionName ).find (Filters .eq ("_id" , "root-1" )).first ();
1198
+ });
1199
+
1200
+ assertThat (target .get ("simpleValueRef" )).isEqualTo (id );
1201
+ }
1202
+
1203
+ @ Test // GH-3847
1204
+ void readWithIdStringThatIsAnObjectId () {
1205
+
1206
+ ObjectId id = new ObjectId ();
1207
+
1208
+ String rootCollectionName = template .getCollectionName (SingleRefRoot .class );
1209
+ String refCollectionName = template .getCollectionName (SimpleObjectRef .class );
1210
+ Document refSource = new Document ("_id" , id ).append ("value" , "me-the-referenced-object" );
1211
+ Document source = new Document ("_id" , "id-1" ).append ("value" , "v1" ).append ("simpleValueRef" , id );
1212
+
1213
+ template .execute (db -> {
1214
+
1215
+ db .getCollection (refCollectionName ).insertOne (refSource );
1216
+ db .getCollection (rootCollectionName ).insertOne (source );
1217
+ return null ;
1218
+ });
1219
+
1220
+ SingleRefRoot result = template .findOne (query (where ("id" ).is ("id-1" )), SingleRefRoot .class );
1221
+ assertThat (result .getSimpleValueRef ()).isEqualTo (new SimpleObjectRef (id .toHexString (), "me-the-referenced-object" ));
1222
+ }
1223
+
1224
+ @ Test // GH-3847
1225
+ void readWriteTypeReferenceHavingFixedStringIdTargetType () {
1226
+
1227
+ ObjectId id = new ObjectId ();
1228
+ String rootCollectionName = template .getCollectionName (SingleRefRoot .class );
1229
+
1230
+ ObjectRefHavingStringIdTargetType customStringIdTargetRef = new ObjectRefHavingStringIdTargetType (id .toHexString (),
1231
+ "me-the-referenced-object" );
1232
+ template .save (customStringIdTargetRef );
1233
+
1234
+ SingleRefRoot source = new SingleRefRoot ();
1235
+ source .id = "root-1" ;
1236
+ source .customStringIdTargetRef = customStringIdTargetRef ;
1237
+ template .save (source );
1238
+
1239
+ Document target = template .execute (db -> {
1240
+ return db .getCollection (rootCollectionName ).find (Filters .eq ("_id" , "root-1" )).first ();
1241
+ });
1242
+
1243
+ assertThat (target .get ("customStringIdTargetRef" )).isEqualTo (id .toHexString ());
1244
+
1245
+ SingleRefRoot result = template .findOne (query (where ("id" ).is ("root-1" )), SingleRefRoot .class );
1246
+ assertThat (result .getCustomStringIdTargetRef ())
1247
+ .isEqualTo (new ObjectRefHavingStringIdTargetType (id .toHexString (), "me-the-referenced-object" ));
1248
+ }
1249
+
1183
1250
@ Data
1184
1251
static class SingleRefRoot {
1185
1252
@@ -1188,7 +1255,7 @@ static class SingleRefRoot {
1188
1255
1189
1256
@ DocumentReference SimpleObjectRefWithReadingConverter withReadingConverter ;
1190
1257
1191
- @ DocumentReference (lookup = "{ '_id' : ' ?#{#target}' }" ) //
1258
+ @ DocumentReference (lookup = "{ '_id' : ?#{#target} }" ) //
1192
1259
SimpleObjectRef simpleValueRef ;
1193
1260
1194
1261
@ DocumentReference (lookup = "{ '_id' : '?#{#target}' }" , lazy = true ) //
@@ -1211,6 +1278,8 @@ static class SingleRefRoot {
1211
1278
ObjectRefOnNonIdField lazyObjectValueRefOnNonIdFields ;
1212
1279
1213
1280
@ DocumentReference ObjectRefHavingCustomizedIdTargetType customIdTargetRef ;
1281
+
1282
+ @ DocumentReference ObjectRefHavingStringIdTargetType customStringIdTargetRef ;
1214
1283
}
1215
1284
1216
1285
@ Data
@@ -1325,6 +1394,14 @@ static class ObjectRefHavingCustomizedIdTargetType {
1325
1394
String name ;
1326
1395
}
1327
1396
1397
+ @ Data
1398
+ @ AllArgsConstructor
1399
+ static class ObjectRefHavingStringIdTargetType {
1400
+
1401
+ @ MongoId (targetType = FieldType .STRING ) String id ;
1402
+ String name ;
1403
+ }
1404
+
1328
1405
static class ReferencableConverter implements Converter <ReferenceAble , DocumentPointer <Object >> {
1329
1406
1330
1407
@ Nullable
0 commit comments