javamongodbenumsmongodb-javamorphia

Morphia throws error when the field was changed its type from String to enum: No enum constant


In DAO class I use with Morphia I have replaced the String filed type to enums.Currency and I expect that everything will work fine, when the data is retrieved from the db:

was

    public String currency;

now

    public Currency currency;

where Currency is:

public enum Currency {
    EUR, USD, RUB
}

I expect that nothing should change, however, I'm getting the error message like that:

Caused by: java.util.concurrent.CompletionException: java.lang.RuntimeException: java.lang.IllegalArgumentException: No enum constant enums.Currency.
        at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:314)
        at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:319)
        at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773)
        ... 6 common frames omitted
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: No enum constant enums.Currency.
        at dev.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:84)
        at dev.morphia.mapping.Mapper.readMappedField(Mapper.java:906)
        at dev.morphia.mapping.Mapper.fromDb(Mapper.java:308)
        at dev.morphia.mapping.Mapper.fromDBObject(Mapper.java:215)
        at dev.morphia.query.internal.MorphiaCursor.next(MorphiaCursor.java:87)
        at dev.morphia.query.internal.MorphiaCursor.toList(MorphiaCursor.java:57)
         ... 6 common frames omitted
Caused by: java.lang.IllegalArgumentException: No enum constant enums.Currency.
        at java.base/java.lang.Enum.valueOf(Enum.java:264)
        at dev.morphia.converters.EnumConverter.decode(EnumConverter.java:19)
        at dev.morphia.converters.Converters.fromDBObject(Converters.java:127)
        at dev.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:19)
        at dev.morphia.mapping.Mapper.readMappedField(Mapper.java:906)
        at dev.morphia.mapping.Mapper.fromDb(Mapper.java:308)
        at dev.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:73)
        ... 35 common frames omitted

What would be the reason and why string values are not just interpreted as enum values?


Solution

  • This error is raised when there are String values in the Mongodb not existing in the enum.

    Here I have realized there are db entries with empty string fields:

    enter image description here

    After replacing the empty string values in the Mongo Db to those with one of enum, the error has gone.