19
19
import java .lang .reflect .Array ;
20
20
import java .lang .reflect .InvocationTargetException ;
21
21
import java .math .BigInteger ;
22
- import java .util .*;
22
+ import java .util .ArrayList ;
23
+ import java .util .Arrays ;
24
+ import java .util .Collection ;
25
+ import java .util .LinkedHashMap ;
26
+ import java .util .List ;
27
+ import java .util .Map ;
23
28
24
29
import com .mongodb .BasicDBList ;
25
30
import com .mongodb .BasicDBObject ;
31
+ import com .mongodb .DB ;
26
32
import com .mongodb .DBObject ;
27
33
import com .mongodb .DBRef ;
34
+ import com .mongodb .Mongo ;
28
35
import org .apache .commons .logging .Log ;
29
36
import org .apache .commons .logging .LogFactory ;
30
37
import org .bson .types .ObjectId ;
40
47
import org .springframework .data .mapping .AssociationHandler ;
41
48
import org .springframework .data .mapping .MappingBeanHelper ;
42
49
import org .springframework .data .mapping .PropertyHandler ;
43
- import org .springframework .data .mapping .model .*;
50
+ import org .springframework .data .mapping .model .Association ;
51
+ import org .springframework .data .mapping .model .MappingContext ;
52
+ import org .springframework .data .mapping .model .MappingException ;
53
+ import org .springframework .data .mapping .model .PersistentEntity ;
54
+ import org .springframework .data .mapping .model .PersistentProperty ;
55
+ import org .springframework .data .mapping .model .PreferredConstructor ;
44
56
import org .springframework .expression .Expression ;
45
57
import org .springframework .expression .spel .standard .SpelExpressionParser ;
46
58
import org .springframework .expression .spel .support .StandardEvaluationContext ;
@@ -59,6 +71,8 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
59
71
protected ApplicationContext applicationContext ;
60
72
protected boolean autowirePersistentBeans = false ;
61
73
protected boolean useFieldAccessOnly = true ;
74
+ protected Mongo mongo ;
75
+ protected String defaultDatabase ;
62
76
63
77
public MappingMongoConverter () {
64
78
initializeConverters ();
@@ -87,6 +101,22 @@ public void setMappingContext(MappingContext mappingContext) {
87
101
this .mappingContext = mappingContext ;
88
102
}
89
103
104
+ public Mongo getMongo () {
105
+ return mongo ;
106
+ }
107
+
108
+ public void setMongo (Mongo mongo ) {
109
+ this .mongo = mongo ;
110
+ }
111
+
112
+ public String getDefaultDatabase () {
113
+ return defaultDatabase ;
114
+ }
115
+
116
+ public void setDefaultDatabase (String defaultDatabase ) {
117
+ this .defaultDatabase = defaultDatabase ;
118
+ }
119
+
90
120
public boolean isAutowirePersistentBeans () {
91
121
return autowirePersistentBeans ;
92
122
}
@@ -369,10 +399,8 @@ protected void writePropertyInternal(PersistentProperty prop, Object obj, DBObje
369
399
Collection <?> coll = (type .isArray () ? Arrays .asList ((Object []) obj ) : (Collection <?>) obj );
370
400
for (Object propObjItem : coll ) {
371
401
if (null != dbref ) {
372
- DBObject dbRefObj = createDBRef (propObjItem , dbref );
373
- if (null != dbRefObj ) {
374
- dbList .add (dbRefObj );
375
- }
402
+ DBRef dbRef = createDBRef (propObjItem , dbref );
403
+ dbList .add (dbRef );
376
404
} else {
377
405
BasicDBObject propDbObj = new BasicDBObject ();
378
406
write (propObjItem , propDbObj , mappingContext .getPersistentEntity (prop .getTypeInformation ()));
@@ -386,10 +414,8 @@ protected void writePropertyInternal(PersistentProperty prop, Object obj, DBObje
386
414
dbo .put (name , mapDbObj );
387
415
} else {
388
416
if (null != dbref ) {
389
- DBObject dbRefObj = createDBRef (obj , dbref );
390
- if (null != dbRefObj ) {
391
- dbo .put (name , dbRefObj );
392
- }
417
+ DBRef dbRef = createDBRef (obj , dbref );
418
+ dbo .put (name , dbRef );
393
419
} else {
394
420
BasicDBObject propDbObj = new BasicDBObject ();
395
421
write (obj , propDbObj , mappingContext .getPersistentEntity (prop .getTypeInformation ()));
@@ -426,36 +452,37 @@ protected void writeMapInternal(Map<Object, Object> obj, DBObject dbo) {
426
452
}
427
453
}
428
454
429
- protected DBObject createDBRef (Object target , org .springframework .data .document .mongodb .mapping .DBRef dbref ) {
455
+ protected DBRef createDBRef (Object target , org .springframework .data .document .mongodb .mapping .DBRef dbref ) {
430
456
PersistentEntity <?> targetEntity = mappingContext .getPersistentEntity (target .getClass ());
431
457
if (null == targetEntity || null == targetEntity .getIdProperty ()) {
432
458
return null ;
433
459
}
434
460
435
- DBObject dbo = new BasicDBObject ();
436
461
PersistentProperty idProperty = targetEntity .getIdProperty ();
437
462
ObjectId id = null ;
438
463
try {
439
464
id = MappingBeanHelper .getProperty (target , idProperty , ObjectId .class , useFieldAccessOnly );
465
+ if (null == id ) {
466
+ throw new MappingException ("Cannot create a reference to an object with a NULL id." );
467
+ }
440
468
} catch (IllegalAccessException e ) {
441
469
throw new MappingException (e .getMessage (), e );
442
470
} catch (InvocationTargetException e ) {
443
471
throw new MappingException (e .getMessage (), e );
444
472
}
445
- dbo .put ("$id" , id );
446
473
447
474
String collection = dbref .collection ();
448
475
if ("" .equals (collection )) {
449
476
collection = targetEntity .getType ().getSimpleName ().toLowerCase ();
450
477
}
451
- dbo .put ("$ref" , collection );
452
478
453
- String db = dbref .db ();
454
- if (! "" .equals (db )) {
455
- dbo . put ( "$db" , db ) ;
479
+ String dbname = dbref .db ();
480
+ if ("" .equals (dbname )) {
481
+ dbname = defaultDatabase ;
456
482
}
457
483
458
- return dbo ;
484
+ DB db = mongo .getDB (dbname );
485
+ return new DBRef (db , collection , id );
459
486
}
460
487
461
488
@ SuppressWarnings ({"unchecked" })
@@ -510,13 +537,9 @@ protected Object getValueInternal(PersistentProperty prop, DBObject dbo, Standar
510
537
511
538
// It's a complex object, have to read it in
512
539
if (dbo .containsField ("_class" )) {
513
- try {
514
- Class <?> clazz = Class .forName (dbo .get ("_class" ).toString ());
515
- dbo .removeField ("_class" );
516
- o = read (clazz , (DBObject ) dbObj );
517
- } catch (ClassNotFoundException e ) {
518
- throw new MappingException (e .getMessage (), e );
519
- }
540
+ Class <?> toType = findTypeToBeUsed ((DBObject ) dbObj );
541
+ dbo .removeField ("_class" );
542
+ o = read (toType , (DBObject ) dbObj );
520
543
} else {
521
544
o = read (mappingContext .getPersistentEntity (prop .getTypeInformation ()), (DBObject ) dbObj );
522
545
}
0 commit comments