Deepak Agrawal
Deepak Agrawal

Reputation: 1301

spring data mongodb Cannot perform cascade save on child object without id set

I am using @CascadeSave to save child object in separate collection. My Document classes are :

public class FbUserProfile{

    @Id
    private long id;

    @DBRef(lazy=true)
    @CascadeSave()
    private Set<FacebookFriend> friends;

    @DBRef(lazy=true)
    @CascadeSave()
    private Set<FacebookFriendList> customFriendList;
}

public class FacebookFriend{
    @Id
    private long id;
    private String name;
}

public class FacebookFriendList{

    @Id
    private long id;
    private String name;
    private String list_type;
}

I add some object in both friends,customFriendList. and try to update fbUserProfile object using:

mongoTemplate.save(fbUserProfile);

note: fbUserProfile already exists in db. Now I am updating this

Error Message: Cannot perform cascade save on child object without id set

If I remove @CascadeSave. It works fine for me. How I can Cascade set objects. I am also using @CascadeSave with other objects. Its working fine but they are not set object.

Upvotes: 3

Views: 4685

Answers (4)

Kyo Huu
Kyo Huu

Reputation: 578

Okey I extend the class and it will check if the document is exist if it exist it will update the document else it insert the document:

@Component
class GenericCascadeMongo(
        private val mongoTemplate: MongoTemplate
) : AbstractMongoEventListener<Any>() {

    override fun onBeforeConvert(event: BeforeConvertEvent<Any?>) {
        val source = event.source
                ?: return
        ReflectionUtils.doWithFields(source.javaClass) { field ->
            ReflectionUtils.makeAccessible(field)
            if (field.isAnnotationPresent(DBRef::class.java) && field.isAnnotationPresent(CascadeSave::class.java)) {

                val fieldValue = field[source]
                        ?: return@doWithFields

                if (fieldValue is List<*>) {
                    fieldValue.filterNotNull().forEach {
                        checkAndSave(it)
                    }
                } else {
                    checkAndSave(fieldValue)
                }
            }
        }
    }

    private fun checkAndSave(fieldValue: Any) {
        try {
            val callback = DbRefFieldCallback(fieldValue)
            ReflectionUtils.doWithFields(fieldValue.javaClass, callback)
            if (!callback.isIdFound && callback.id == null) {
                mongoTemplate.insert(fieldValue)
            }

            if (callback.id != null) {
                val findById = mongoTemplate.exists(Query(Criteria.where(MConst.MONGO_ID).`is`(callback.id)), fieldValue.javaClass)
                if (findById) {
                    mongoTemplate.save(fieldValue)
                } else {
                    mongoTemplate.insert(fieldValue)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private class DbRefFieldCallback(val fieldValue: Any?) : FieldCallback {
        var isIdFound = false
            private set
        var id: String? = null
            private set

        @Throws(IllegalArgumentException::class, IllegalAccessException::class)
        override fun doWith(field: Field) {
            ReflectionUtils.makeAccessible(field)
            if (field.isAnnotationPresent(Id::class.java)) {
                isIdFound = true
                id = ReflectionUtils.getField(field, fieldValue)?.toString()
            }
        }
    }
}

Upvotes: 0

afarrapeira
afarrapeira

Reputation: 750

I found the same tutorials somewhere else: Baeldung's and JavaCodeGeeks (this is the one i've followed)

I've had that same problem, and I could solve it.

It happens when you try to persist a collection. It doesn't matter that the collection's items have the @Id, because the collection itself won't have it. I edited the code in the EventListener's onBeforeConvert to check if the field you're trying to CascadeSave is a collection (in my case a List). If it's a list, you just cycle through it checking each individual item for @Id and saving them.

If it's not a collection you still have to persist them the same way you did before

@Override
public void onBeforeConvert(Object source) {
    ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {

        @Override
        public void doWith(Field field)
                throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)){
                final Object fieldValue = field.get(source);

                if(fieldValue instanceof List<?>){
                    for (Object item : (List<?>)fieldValue){
                        checkNSave(item);
                    }
                }else{
                    checkNSave(fieldValue);
                }
            }
        }
    });
}

private void checkNSave(Object fieldValue){
    DbRefFieldCallback callback = new DbRefFieldCallback();
    ReflectionUtils.doWithFields(fieldValue.getClass(), callback);

    if (!callback.isIdFound()){
        throw new MappingException("Oops, something went wrong. Child doesn't have @Id?");
    }

    mongoOperations.save(fieldValue);
}

Upvotes: 4

S.patil
S.patil

Reputation: 131

The above solution works fine incase if you have a list. But we can avoid firing a save query for each element from the list, as it reduces the performance. Here is the solution I have found out of the above code.

 @Override
  public void onBeforeConvert(BeforeConvertEvent<Object> event) {
      Object source = event.getSource(); 
      ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {

          @Override
          public void doWith(Field field)
                  throws IllegalArgumentException, IllegalAccessException {
              ReflectionUtils.makeAccessible(field);

              if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)){
                  final Object fieldValue = field.get(source);

                  if(fieldValue instanceof List<?>){
                      for (Object item : (List<?>)fieldValue){
                          checkNAdd(item);
                      }
                  }else{
                      checkNAdd(fieldValue);
                  }
                  mongoOperations.insertAll(documents);
              }
          }
      });
  }

  private void checkNAdd(Object fieldValue){
      DbRefFieldCallback callback = new DbRefFieldCallback();
      ReflectionUtils.doWithFields(fieldValue.getClass(), callback);

      if (!callback.isIdFound()){
          throw new MappingException("Oops, something went wrong. Child doesn't have @Id?");
      }

      documents.add(fieldValue);
  }

Upvotes: 2

Satyam
Satyam

Reputation: 761

The best way to set an ID on the dependent child object is to write a listener class by extending AbstractMongoEventListener class and override the onConvert() method.

public class CustomMongoEventListener extends
        AbstractMongoEventListener<Object> {

    @Autowired
    private MongoOperations mongoOperations;

    @Override
    public void onBeforeConvert(final Object entity) {

    if (entity.id == null || entity.id.isEmpty()) {
      entity.id =   generateGuid();   //generate random sequence ID
    }

public static String generateGuid() {
        SecureRandom randomGen = new SecureRandom();
        byte[] byteArray = new byte[16];
        randomGen.nextBytes(byteArray);
        return new Base32().encodeToString(byteArray).substring(0,26);
    }

}

Finally register your custom listener in `your configuration file. For annotation approach use the following code to register :
@Bean
    public CustomMongoEventListener cascadingMongoEventListener() {
        return new CustomMongoEventListener();
    }

Upvotes: 2

Related Questions