Version :
Quarkus : 3.20.1
Morphia : 2.5.0
Java : 21
Mongo Driver : 5.3.1
Condition describe :
I want to retrieve an entity from a collection that contains three different types of entity structures (all inheriting from a common base class). However, an error occurs during deserialization, and I’m unable to resolve the issue.
By the way, I was able to save the entity successfully
To be honest, I’m not sure if my current architecture and the way I’m performing the query are even valid for this use case.
I came across this related post: [link], but it seems there hasn’t been any update or resolution since then.
Q1 : Error message like this
2025-06-09 18:24:19,893 ERROR [com.xxx.con.GeneralExceptionMapper] (executor-thread-1) Failed to decode ‘AbstractSnapshotEntity’. Decoding errored with: A class could not be found for the discriminator: ‘SnapshotProductEntity’.: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode ‘AbstractSnapshotEntity’. Decoding errored with: A class could not be found for the discriminator: ‘Snap1Entity’.
at dev.morphia.mapping.codec.pojo.EntityDecoder.getCodecFromDocument(EntityDecoder.java:111)
at dev.morphia.mapping.codec.pojo.EntityDecoder.decode(EntityDecoder.java:51)
at dev.morphia.mapping.codec.pojo.MorphiaCodec.decode(MorphiaCodec.java:76)
at com.mongodb.internal.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
… … … … … … … … … …
… … … … … … … … … …
Caused by: org.bson.codecs.configuration.CodecConfigurationException: A class could not be found for the discriminator: ‘Snap1Entity’.
at dev.morphia.mapping.DiscriminatorLookup.lookup(DiscriminatorLookup.java:79)
at dev.morphia.mapping.codec.pojo.EntityDecoder.getCodecFromDocument(EntityDecoder.java:105)
… 60 more
Q2 : While debugging the mapping process (possibly related to class resolution), I noticed thatClass.forName(classInfo.getName())always throws an exception.
However, when I run the same code in my own application context, the class loads without any issues.
I’m not sure if this is related to how the class loader is handling things in the Morphia context.
Datastore setup
@Produces
@ApplicationScoped
Datastore getDatastore(MongoClient client) throws ClassNotFoundException {
String packagePath = "com.xxx.entity.nosql";
MorphiaConfig config = new ManualMorphiaConfig()
.database(datastoreProperties.getMongoDbDatabaseName())
.packages(List.of(packagePath))
.storeNulls(true)
.storeEmpties(true)
.enablePolymorphicQueries(true);
Datastore datastore = Morphia.createDatastore(client, config);
datastore.getMapper().getMappedEntities().forEach(
entityModel -> log.info("✅ Morphia registered entity: {}", entityModel.getCollectionName())
);
return datastore;
}
Entity structure like below
public class NoSqlBaseEntity {
@Id
private ObjectId id;
private LocalDateTime createdDate;
private LocalDateTime updatedDate;
}
@Entity(value = "Snapshot")
public class AbstractSnapshotEntity extends NoSqlBaseEntity {
private DataType dataType;
private String code;
private String hub;
}
@Entity(value = "Snapshot")
public class Snap1Entity extends AbstractSnapshotEntity {
private Snap1 snapshotData; // temporary
}
@Entity(value = "Snapshot")
public class Snap2Entity extends AbstractSnapshotEntity {
private Snap2 snapshotData; // temporary
}
@Entity(value = "Snapshot")
public class Snap3Entity extends AbstractSnapshotEntity {
private Snap3 snapshotData; // temporary
}
Repository
@Inject
private Datastore datastore;
..............................
..............................
public Optional<AbstractSnapshotEntity> findById(String id){
return datastore.find(AbstractSnapshotEntity.class).
filter(
Filters.eq("_id", new ObjectId(snapshotId))
)
.stream()
.findFirst();
}
Real Data Sample
Thank you for the support.
Hopefully, resolving this issue will also help others encountering the same situation.

