Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auditing exception when nested entities in collection type filed have audit fields [DATACMNS-1519] #1949

Closed
spring-projects-issues opened this issue Apr 22, 2019 · 4 comments
Assignees
Labels
in: mapping status: declined type: bug

Comments

@spring-projects-issues
Copy link

spring-projects-issues commented Apr 22, 2019

revivn opened DATACMNS-1519 and commented

Hi. I'm trying to add auto auditing into spring project using spring-data-commons,spring-data-mongodb,both version 2.1.3.RELEASE

  1. Enable mongo auditing in my spring project's mongodbConfig by using @EnableMongoAuditing.
  2. Add an abstract entity(AbstractEntity) with @CreatedDate@LastModifiedDate, @CreatedBy, and @LastModifiedBy annotations.
  3. Add a sub object(EntityB) extends AbstractEntity,EntityB also have other ordinary fields.
  4. Add another object(EntityA) extends AbstractEntity,EntityA have other ordinary fields and a collection filed type of List<EntityB>
  5. Try saving EntityA's instance with collection filed type of List<EntityB> contains some EntityB's instance by calling the MongoRepository's save() method.
  6. The error(java.lang.IllegalArgumentException) should display(Target bean of type java.util.ArrayList is not of type of the persistent entity (EntityB)!: java.util.ArrayList).

here's the data structure

!https://raw.githubusercontent.com/gbfvip/docs/master/nested%20entities.png!

here's the exception stack trace:

java.lang.Thread.State: RUNNABLE
     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$1130.982668958.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$1129.953422663.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$1123.1510646824.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$1095.2043125926.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$1094.398724568.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$990.1881179642.apply(Unknown Source:-1)

I found that method

line:56  setProperty(..)

in

PersistentPropertyAccessor

may have defect in this nested collection field scenario:

line:64 Object parent = parentPath.isEmpty() ? getBean() : getProperty(parentPath)

while object "parent" get the collection type of List<EntityB>

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

1.leafProperty is EntityB's annotated property field

2.leafProperty.getOwner() get PersistenceEntity of EntityB

3.leafProperty.getOwner().getPropertyAccessor(parent) try to get audit fileld's accessor of the given bean(which is List<entityB>) from PersistenceEntity of EntityB

4.IllegalArgumentException occurred

 

I've checked github's master branch and seems latest code still have the same behavior


Reference URL: https://stackoverflow.com/questions/55791005/how-do-i-using-mongodb-auditing-with-nested-auditable-objects

Issue Links:

  • DATACMNS-1438 Annotation based auditing fails for annotation properties underneath collection valued subpath
    ("duplicates")
@spring-projects-issues
Copy link
Author

spring-projects-issues commented Apr 23, 2019

Oliver Drotbohm commented

This looks like a duplicate of DATACMNS-1438 which was fixed in 2.1.4. Would mind giving a more recent version a try?

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Apr 23, 2019

revivn commented

thanks Oliver,I'll try newer version and get back to you with result

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Apr 24, 2019

revivn commented

Hi Oliver,I found that v2.1.4.RELEASE still not fullfill my question,please correct me if I'm wrong

from v2.1.4.RELEASE, seems only first level entity's audit fields will be audit(EntityA's audit fields),nested property no longer be audit(EntityB in List<EntityB>),is this done on purpose?anyway I can get all entities be audit(entityA.entityB)?


thanks Oliver,I got what I need after careful study of DATACMNS-1438

in general,upcoming v2.2 will take care of nested collection property,for now nested collection will be skipped

@spring-projects-issues
Copy link
Author

spring-projects-issues commented May 9, 2019

Oliver Drotbohm commented

Already fixed in 2.1.4, to a more extensive extend in 2.2

@spring-projects-issues spring-projects-issues added type: bug status: declined in: mapping labels Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping status: declined type: bug
Projects
None yet
Development

No branches or pull requests

2 participants