Merge pull request #240 from alex-semenyuk/master

Fixed test with @transient and created separate callback
This commit is contained in:
Eugen 2015-08-23 08:03:40 -07:00
commit c9aba4eeff
3 changed files with 60 additions and 25 deletions

View File

@ -0,0 +1,53 @@
package org.baeldung.event;
import java.lang.reflect.Field;
import org.baeldung.annotation.CascadeSave;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.util.ReflectionUtils;
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(Field field) throws IllegalArgumentException, IllegalAccessException {
ReflectionUtils.makeAccessible(field);
if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
final Object fieldValue = field.get(getSource());
if (fieldValue != null) {
FieldCallback callback = new FieldCallback();
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
getMongoOperations().save(fieldValue);
}
}
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
public MongoOperations getMongoOperations() {
return mongoOperations;
}
public void setMongoOperations(MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
}

View File

@ -1,37 +1,17 @@
package org.baeldung.event;
import org.baeldung.annotation.CascadeSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
@Autowired
private MongoOperations mongoOperations;
@Override
public void onBeforeConvert(final Object source) {
ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {
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 != null) {
FieldCallback callback = new FieldCallback();
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
mongoOperations.save(fieldValue);
}
}
}
});
ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
}
}

View File

@ -2,6 +2,7 @@ package org.baeldung.mongotemplate;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.nullValue;
import java.util.List;
@ -25,6 +26,7 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class MongoTemplateQueryIntegrationTest {
@ -189,6 +191,6 @@ public class MongoTemplateQueryIntegrationTest {
user.setYearOfBirth(1985);
mongoTemplate.insert(user);
assertThat(user.getYearOfBirth(), is(1985));
assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getYearOfBirth(), is(nullValue()));
}
}