如何将MongoDB审核与集合类型字段中的嵌套可审核对象一起正确使用

时间:2019-04-22 07:51:20

标签: spring-data-mongodb spring-data-commons

我在项目中使用@EnableMongoAuditing和其他4个批注(@LastModifiedDate ...),当我仅使用单个实体时,它们可以完美工作,但是在使用neteted实体时会出错

nested entities

说我有这个实体数据结构,EntityA和EntityB都扩展了AbstractEntity(意味着它们都有自己的审核字段)。EntityA的集合字段包含EntityB对象。

将EntityA实例保存到数据库中时发生“ java.lang.IllegalArgumentException”异常,堆栈跟踪:

  at org.springframework.util.Assert.instanceCheckFailed(Assert.java:655)
  at org.springframework.util.Assert.isInstanceOf(Assert.java:574)
  at org.springframework.data.mapping.model.BasicPersistentEntity.verifyBeanType(BasicPersistentEntity.java:551)
  at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:453)
  at org.springframework.data.mapping.PersistentPropertyAccessor.setProperty(PersistentPropertyAccessor.java:76)
  at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.lambda$setDateProperty$2(MappingAuditableBeanWrapperFactory.java:243)
  at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper$$Lambda$1129.335811674.accept(Unknown Source:-1)
  at java.lang.Iterable.forEach(Iterable.java:75)
  at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.setDateProperty(MappingAuditableBeanWrapperFactory.java:243)
  at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.setLastModifiedDate(MappingAuditableBeanWrapperFactory.java:220)
  at org.springframework.data.auditing.AuditingHandler.lambda$touchDate$11(AuditingHandler.java:220)
  at org.springframework.data.auditing.AuditingHandler$$Lambda$1128.164899054.accept(Unknown Source:-1)
  at java.util.Optional.ifPresent(Optional.java:159)
  at org.springframework.data.auditing.AuditingHandler.touchDate(AuditingHandler.java:220)
  at org.springframework.data.auditing.AuditingHandler.lambda$touch$0(AuditingHandler.java:166)
  at org.springframework.data.auditing.AuditingHandler$$Lambda$1122.754120807.apply(Unknown Source:-1)
  at java.util.Optional.map(Optional.java:215)
  at org.springframework.data.auditing.AuditingHandler.touch(AuditingHandler.java:163)
  at org.springframework.data.auditing.AuditingHandler.markModified(AuditingHandler.java:143)
  at org.springframework.data.auditing.IsNewAwareAuditingHandler.markAudited(IsNewAwareAuditingHandler.java:83)
  at org.springframework.data.mongodb.core.mapping.event.AuditingEventListener.lambda$onApplicationEvent$0(AuditingEventListener.java:54)
  at org.springframework.data.mongodb.core.mapping.event.AuditingEventListener$$Lambda$1094.1600079218.apply(Unknown Source:-1)
  at org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent.mapSource(MongoMappingEvent.java:90)
  at org.springframework.data.mongodb.core.mapping.event.AuditingEventListener.onApplicationEvent(AuditingEventListener.java:54)
  at org.springframework.data.mongodb.core.mapping.event.AuditingEventListener.onApplicationEvent(AuditingEventListener.java:32)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
  at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398)
  at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355)
  at org.springframework.data.mongodb.core.ReactiveMongoTemplate.maybeEmitEvent(ReactiveMongoTemplate.java:2407)
  at org.springframework.data.mongodb.core.ReactiveMongoTemplate.lambda$doSave$35(ReactiveMongoTemplate.java:1428)
  at org.springframework.data.mongodb.core.ReactiveMongoTemplate$$Lambda$1093.1542796792.doInCollection(Unknown Source:-1)
  at org.springframework.data.mongodb.core.ReactiveMongoTemplate.lambda$createMono$8(ReactiveMongoTemplate.java:599)
  at org.springframework.data.mongodb.core.ReactiveMongoTemplate$$Lambda$989.249044352.apply(Unknown Source:-1)

错误消息: “类型为java.util.ArrayList的目标bean不是持久实体(EntityB)的类型!:java.util.ArrayList”

如何正确地对EntityA(包含集合EntityB)进行自动审核?


发现:

PersistentPropertyAccessor为每个带注释的字段设置属性,并且此字段属于集合类型时,setProperty中的那些代码似乎是错误的:

Object parent = parentPath.isEmpty() ? getBean() : getProperty(parentPath);//1

        if (parent == null) {

            String nullIntermediateMessage = "Cannot lookup property %s on null intermediate! Original path was: %s on %s.";

            throw new MappingException(String.format(nullIntermediateMessage, parentPath.getLeafProperty(), path.toDotPath(),
                    getBean().getClass().getName()));
        }

        PersistentPropertyAccessor<?> accessor = parent == getBean() //
                ? this //
                : leafProperty.getOwner().getPropertyAccessor(parent);//2

1。说“ entityA.entityB” parentPath不为空且属于collection类型,因此parent参数成为一个collection。

2。在这里应该为每个项目迭代parent和getPropertyAccessor吗?

0 个答案:

没有答案