implementation of cascade save in spring data and mongodb
<p>I'm implementing a application based on spring data and mongodb. </p>

<h2>QATemplate.java</h2>

<pre><code>@Document(collection = "QATemplate")
public class QATemplate {

    @Id
    private String titleId;

    public String getTitleId() {
        return titleId;
    }

    public void setTitleId(String titleId) {
        this.titleId = titleId;
    }

    @Field("title")
    private String title;

    @Field("tags")
    private Object tags;

    @Field("answer_type")
    private String answerType;

    @Field("answer")
    @DBRef
    @CascadeSave
    private Object answer;
</code></pre>

<p>i have a field which can be simple text or can be embedded document. 
so i created that field as a object type.</p>

<pre><code>    @Field("answer")
    private Object answer;
</code></pre>

<p>It worked fine.
but i need to save embedded document with its own id and pass its id as reference into this document so i have to write my code like this</p>

<pre><code>    @Field("answer")
    @DBRef
    @CascadeSave
    private Object answer;
</code></pre>

<p>for implementation of cascade save i used</p>

<h2>CasecadeSave.java</h2>

<pre><code>@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CascadeSave {

}
</code></pre>

<h2>CasecadeCallback.java</h2>

<pre><code>public class CascadeCallback implements ReflectionUtils.FieldCallback {

    private Object source;
    private MongoOperations mongoOperations;

    public CascadeCallback(final Object source, final MongoOperations mongoOperations) {
        this.source = source;
        this.setMongoOperations(mongoOperations);
    }

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

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

            boolean insta = !(fieldValue instanceof String);
            boolean instanull = fieldValue != null;

            if ( instanull &amp;&amp; insta ) {
                final FieldCallback callback = new FieldCallback();
                ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
                getMongoOperations().save(fieldValue);
            }
        }
    }

    public Object getSource() {
        return source;
    }

    public void setSource(final Object source) {
        this.source = source;
    }

    public MongoOperations getMongoOperations() {
        return mongoOperations;
    }

    public void setMongoOperations(final MongoOperations mongoOperations) {
        this.mongoOperations = mongoOperations;
    }
}
</code></pre>

<h2>CascadeSaveMongoEventListener.java</h2>

<pre><code>public class CascadeSaveMongoEventListener extends AbstractMongoEventListener&lt;Object&gt; {

    @Autowired
    private MongoOperations mongoOperations;

    @Override
    public void onBeforeConvert(final Object source) {
        ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
    }
  }
</code></pre>

<h2>FieldCallback.java</h2>

<pre><code>public class FieldCallback implements ReflectionUtils.FieldCallback {
    private boolean idFound;

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

        if (field.isAnnotationPresent(Id.class)) {
            idFound = true;
        }
    }

    public boolean isIdFound() {
        return idFound;
    }
}
</code></pre>

<p>but after applying this code if i insert json data like this</p>

<pre><code>{
    "title":"Indiaa",
    "answerType":"text",
    "answer":[
            {
                 "title":"Indiaana jones",
                 "answerType":"text",
                 "answer":"testing vvv"
            }
        ]
}
</code></pre>

<p>i got an error:</p>

<pre><code>threw exception [Request processing failed; 
nested exception is java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.BasicDBList] with root cause
java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.BasicDBList
</code></pre>

<p>and if i try to insert json data like this:</p>

<pre><code>{
    "title":"Indiava",
    "answerType":"text",
    "answer":"vv"
}
</code></pre>

<p>I got an error:</p>

<pre><code>HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.data.mapping.model.MappingException: No mapping metadata found for class java.lang.String
</code></pre>