2727import io .github .kaiso .relmongo .util .ReflectionsUtil ;
2828import io .github .kaiso .relmongo .util .RelMongoConstants ;
2929
30- import org .bson .types .ObjectId ;
3130import org .springframework .data .mongodb .core .MongoOperations ;
32- import org .springframework .data .mongodb .core .mapping .Document ;
3331import org .springframework .util .ReflectionUtils ;
3432import org .springframework .util .ReflectionUtils .FieldCallback ;
3533import org .springframework .util .StringUtils ;
@@ -74,7 +72,7 @@ private void saveAssociation(Field field, CascadeType cascadeType, Boolean orpha
7472 String name = AnnotationsUtils .getJoinProperty (field );
7573 Object reference = null ;
7674 reference = ((org .bson .Document ) source ).get (field .getName ());
77- String childCollectionName = getCollectionName (field );
75+ String childCollectionName = AnnotationsUtils . getCollectionName (field );
7876 if (reference instanceof BasicDBList ) {
7977 BasicDBList list = new BasicDBList ();
8078 list .addAll (((BasicDBList ) reference ).stream ()
@@ -84,7 +82,7 @@ private void saveAssociation(Field field, CascadeType cascadeType, Boolean orpha
8482 ((org .bson .Document ) source ).put (name , list );
8583 if (Boolean .TRUE .equals (orphanRemoval )) {
8684 removeOrphans (((org .bson .Document ) source ).get ("_id" ),
87- list .parallelStream ().map (o -> (ObjectId ) ( (org .bson .Document ) o ).get ("_id" )).collect (Collectors .toList ()),
85+ list .parallelStream ().map (o -> ((org .bson .Document ) o ).get ("_id" )).collect (Collectors .toList ()),
8886 name ,
8987 field );
9088 }
@@ -105,19 +103,12 @@ private org.bson.Document keepOnlyIdentifier(Object obj, String collection, Casc
105103 Object objectId = ((org .bson .Document ) obj ).get ("_id" );
106104 if (objectId == null && !Arrays .asList (CascadeType .PERSIST , CascadeType .ALL ).contains (cascadeType )) {
107105 throw new RelMongoProcessingException (
108- "ObjectId must not be null when persisting without cascade ALL or PERSIST " );
106+ "The entity Id must not be null when persisting without cascade ALL or PERSIST " );
109107 }
110108 return new org .bson .Document ().append ("_id" , objectId ).append (RelMongoConstants .RELMONGOTARGET_PROPERTY_NAME ,
111109 collection );
112110 }
113111
114- private String getCollectionName (Field field ) {
115- String collection = ReflectionsUtil .getGenericType (field ).getAnnotation (Document .class ).collection ();
116- if (StringUtils .isEmpty (collection )) {
117- collection = ReflectionsUtil .getGenericType (field ).getSimpleName ().toLowerCase ();
118- }
119- return collection ;
120- }
121112
122113 @ SuppressWarnings ({ "unchecked" })
123114 private void removeOrphans (Object parentId , List <Object > child , String propertyName , Field field ) {
@@ -129,15 +120,15 @@ private void removeOrphans(Object parentId, List<Object> child, String propertyN
129120 org .bson .Document currentDocument = (org .bson .Document ) result .get (0 );
130121 if (currentDocument != null ) {
131122 Object currentChild = currentDocument .get (propertyName );
132- List <ObjectId > objectsToRemove = null ;
123+ List <Object > objectsToRemove = null ;
133124 if (currentChild instanceof ArrayList ) {
134125 objectsToRemove = new ArrayList <>();
135126 ArrayList <org .bson .Document > childList = (ArrayList <org .bson .Document >) currentChild ;
136127 childList .removeIf (o -> child .contains (o .get ("_id" )));
137- objectsToRemove .addAll (childList .parallelStream ().map (o -> ( ObjectId ) o .get ("_id" ))
128+ objectsToRemove .addAll (childList .parallelStream ().map (o -> o .get ("_id" ))
138129 .collect (Collectors .toList ()));
139130 } else if (currentChild instanceof org .bson .Document ) {
140- ObjectId currentChildId = ( ObjectId ) ((org .bson .Document ) currentChild ).get ("_id" );
131+ Object currentChildId = ((org .bson .Document ) currentChild ).get ("_id" );
141132 if (child .isEmpty () || !currentChildId .equals (child .get (0 ))) {
142133 objectsToRemove = new ArrayList <>();
143134 objectsToRemove .add (currentChildId );
0 commit comments