spring-data-mongodb: BeanInstantiationException: Failed to instantiate []: Specified class is an interface [DATAMONGO-2391]
Ilya Zinkevich opened DATAMONGO-2391 and commented
Hi,
I get error with package org.springframework.boot:spring-boot-starter-data-mongodb:2.1.5.RELEASE
Description:
I have interface Call and class TokBoxCall (implements Call) in the code.
There is default typeKey in the records in Mongo DB:
"_class":"com.xxxxx.server.data.model.call.TokBoxCall".
And I didn’t overwrite it in Spring Mongo Config.
However, when I perform reading operations, e.g. callRepository.findById(id), I get Exception:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxxxx.server.data.model.Call]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:119)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64)}}
Affects: 2.1.11 (Lovelace SR11)
1 votes, 4 watchers
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 3
- Comments: 15 (3 by maintainers)
Thank you @DobryninVyacheslav for the additional context.
EntityScanneris part of spring-boot and using anAnnotationTypeFilterthat by default does not considerinterfacetypes. There’s however an open issue spring-projects/spring-boot#12828 to allow more fine grained filtering. At this point I’m inclined to close this issue in favor of the spring-boot one.Hey @christophstrobl , thanks for the prompt reply!
Yeah, initial entity set seems to do the job, although I’d go with something like this:
Could it have any unexpected side effects though? Is there a chance
FieldTypeImplandOtherFieldTypeImplwill be treated as separate collections in mongo?Yeah, the documentation does mention it, that’s why I came up with a solution like this:
But the thing is, you wouldn’t go to the documentation when having a polymorphic field seems to just work out of the box. Having a test like
SaveAndGetWithRepositoryTestmight create an impression it just works. And the worst thing is that even if you save a document without using repository, you might still have other tests that save it with repository, thus affecting the test via a shared context.I think the behavior should be consistent here: if there is no mapping for the type then it should fail while saving it. It shouldn’t only fail when you didn’t save a document first before getting another one from the DB.