2222import io .github .kaiso .relmongo .mongo .DatabaseOperations ;
2323
2424import org .bson .Document ;
25- import org .bson .types .ObjectId ;
2625import org .springframework .data .mongodb .core .MongoOperations ;
2726import org .springframework .util .ReflectionUtils ;
2827import org .springframework .util .ReflectionUtils .FieldCallback ;
@@ -40,14 +39,14 @@ public class PersistentPropertyCascadingRemoveCallback implements FieldCallback
4039
4140 private Document source ;
4241 private MongoOperations mongoOperations ;
43- private Boolean loaded = Boolean .FALSE ;
4442 private Object entity ;
43+ private Class <?> entityType ;
4544
46- public PersistentPropertyCascadingRemoveCallback (Document source , MongoOperations mongoOperations , Class <?> clazz ) {
45+ public PersistentPropertyCascadingRemoveCallback (Document source , MongoOperations mongoOperations , Class <?> entityType ) {
4746 super ();
4847 this .source = source ;
4948 this .mongoOperations = mongoOperations ;
50- this .entity = mongoOperations . getConverter (). read ( clazz , source ) ;
49+ this .entityType = entityType ;
5150 }
5251
5352 public void doWith (Field field ) throws IllegalAccessException {
@@ -64,7 +63,6 @@ private void doCascade(Field field, CascadeType cascadeType) throws IllegalAcces
6463 if (!Arrays .asList (CascadeType .REMOVE , CascadeType .ALL ).contains (cascadeType )) {
6564 return ;
6665 }
67- loadEntity ();
6866 Object child = field .get (entity );
6967 if (child != null ) {
7068 if (Collection .class .isAssignableFrom (child .getClass ())) {
@@ -87,21 +85,20 @@ private void cascadeCollection(Collection<?> child) {
8785 }
8886
8987 public void doProcessing () {
88+ loadEntity ();
9089 if (entity == null ) {
9190 return ;
9291 }
9392 ReflectionUtils .doWithFields (entity .getClass (), this );
9493 }
9594
9695 private void loadEntity () {
97- if (! loaded ) {
96+ if (entity == null ) {
9897 Object sourceId = this .source .get ("_id" );
9998 if (sourceId != null ) {
100- ObjectId id = sourceId instanceof ObjectId ? (ObjectId ) sourceId : new ObjectId ((String ) sourceId );
101- Collection <?> findByIds = DatabaseOperations .findByIds (mongoOperations , entity .getClass (), id );
99+ Collection <?> findByIds = DatabaseOperations .findByIds (mongoOperations , entityType , sourceId );
102100 this .entity = findByIds != null && !findByIds .isEmpty () ? findByIds .iterator ().next () : null ;
103101 }
104- loaded = Boolean .TRUE ;
105102 }
106103 }
107104
0 commit comments