I have a complex data model that among other things uses abstract classes. Writing the Data Model Objectto MongoDB using the AutomaticPojoCodec works fine, but when trying to read the Data Model Object from MongoDB again, the following error is shown:
org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'ReqIF' failed with the following exception:
Failed to decode 'coreContent'. Failed to decode 'reqIF_content'. Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
null
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:40)
at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:50)
at com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:132)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:111)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:176)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:113)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:715)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:709)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:433)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:709)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:81)
at com.mongodb.Mongo.execute(Mongo.java:810)
at com.mongodb.Mongo$2.execute(Mongo.java:797)
at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:200)
at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:37)
at io.cesonia.camelot.knight.reqIF.mongoDB.ReqIF_MongoDB_Reader.findByAddress(ReqIF_MongoDB_Reader.java:43)
at io.cesonia.camelot.knight.reqIF.mongoDB.ReqIF_MongoDB_Reader.readReqIF_DataFromMongoDB(ReqIF_MongoDB_Reader.java:29)
at io.cesonia.camelot.knight.reqIF.mongoDB.ReqIF_MongoDB_ReaderTest.readReqIF_DataFromMongoDB_shouldReturnReqIF_dataObject(ReqIF_MongoDB_ReaderTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:205)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(Unknown Source)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(Unknown Source)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'coreContent'. Failed to decode 'reqIF_content'. Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
null
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37)
... 97 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'reqIF_content'. Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
null
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:55)
at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
... 101 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'datatypes'. Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
null
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:55)
at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
... 107 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'datatypeDefinitions'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
null
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:55)
at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
... 113 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
null
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:40)
at org.bson.codecs.pojo.CollectionCodec.decode(CollectionCodec.java:53)
at org.bson.codecs.pojo.CollectionCodec.decode(CollectionCodec.java:30)
at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:165)
... 119 more
Caused by: org.bson.codecs.configuration.CodecConfigurationException
at org.bson.codecs.pojo.CreatorExecutable.getInstance(CreatorExecutable.java:89)
at org.bson.codecs.pojo.InstanceCreatorImpl.<init>(InstanceCreatorImpl.java:40)
at org.bson.codecs.pojo.InstanceCreatorFactoryImpl.create(InstanceCreatorFactoryImpl.java:28)
at org.bson.codecs.pojo.ClassModel.getInstanceCreator(ClassModel.java:70)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:102)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37)
... 123 more
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.bson.codecs.pojo.CreatorExecutable.getInstance(CreatorExecutable.java:84)
... 129 more
I believe the relevant lines of this error are the following ones:
Failed to decode 'coreContent'.
Failed to decode 'reqIF_content'.
Failed to decode 'datatypes'.
Failed to decode 'datatypeDefinitions'.
An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'DatatypeDefinitionTag' failed with the following exception:
[…]
Caused by: java.lang.InstantiationException
I assume this happens because ´DatatypeDefinitionTag´ is an abstract class that as such can't and shouldn't be initiatied. It is used to store data about the datatype definitions, and as such may contain a variable number of DatatypeDefinitionString, DatatypeDefinitionDate, DatatypeDefinitionInt etc... Objects.
In my Datatypes class , the faulting variable datatypeDefinitions is defined like this:
public List<DatatypeDefinitionTag> datatypeDefinitions;
...and that variable is filled with objects that look like this:
public class DatatypeDefinitionString extends DatatypeDefinitionTag{[...]}
public class DatatypeDefinitionInt extends DatatypeDefinitionTag{[...]}
public class DatatypeDefinitionDate extends DatatypeDefinitionTag{[...]}
I am reasonably certain that this is what causes the error, since it does not appear anymore if I remove the datatypes from the data object. However, that, naturally, is not a feasible solution.
My question boils down to two things:
- Do I assume correctly that the problem here is that the AutomaticPojoCodec can't decode abstract objects?
- What can I do about it?