简体   繁体   English

级联保存在spring数据和mongodb中

[英]cascade save in spring data and mongodb

I'm implementing an application based on spring data and mongodb. 我正在实现一个基于spring数据和mongodb的应用程序。

@Document
public class User {

    @Id
    private ObjectId id;

    private String name;

    private Integer age;

    @DBRef
    private Address address;

    @DBRef
    @CascadeSave
    private Set<Action> actions = new HashSet<Action>();

i need to save actions list as embedded document into the user, for that i implement custom cascade save 我需要将操作列表作为嵌入式文档保存到用户中,为此我实现了自定义级联保存

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CascadeSave {
}

CascadingMongoEventListener.java CascadingMongoEventListener.java

public class CascadingMongoEventListener extends AbstractMongoEventListener<Object> {

    @Autowired
    private MongoOperations mongoOperations;

    @Override
    public void onBeforeConvert(final BeforeConvertEvent<Object> event) {
        final 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) {
                            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);
    }

    private static class DbRefFieldCallback 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;
        }
    }
}

i try to save a user with a list of actions 我尝试用操作列表保存用户

Action action = new Action();
action.setDescription("des");
action.setTime(ZonedDateTime.now());
User personHektor = new User();
personHektor.setName("Hektor");
personHektor.setActions(Stream.of(action).collect(Collectors.toSet()));
personRepo.save(personHektor);

but i have this error 但是我有这个错误

org.springframework.data.mapping.MappingException: Oops, something went wrong. org.springframework.data.mapping.MappingException:糟糕,出了点问题。 Child doesn't have @Id? 孩子没有@Id?

i pushed the project in github for more visibility 我在github中推送了该项目以提高知名度

CascadingMongoEventListener checks the fieldValue against List though it's actually a Set . 尽管CascadingMongoEventListener实际上是Set但它会根据List检查fieldValue
Change that (to maybe Collection ) and the MappingException will go away. 将其更改(可能是Collection ),然后MappingException将消失。
To additionally solve the issue with ZonedDateTime please register a Converter for the conversion. 为了进一步解决ZonedDateTime的问题,请注册一个Converter以进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM