当字段的类型从字符串更改为枚举时,Morphia 抛出错误:没有枚举常量

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

在 DAO class 中,我使用 Morphia 我已将 String 字段类型替换为 enums.Currency,我希望从数据库中检索数据时一切正常:

    public String currency;

现在

    public Currency currency;

其中货币是:

public enum Currency {
    EUR, USD, RUB
}

我希望不会有任何改变,但是,我收到这样的错误消息:

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

字符串值不只是被解释为枚举值的原因是什么?

当 Mongodb 中的 String 值在 enum 中不存在时会引发此错误。

在这里我发现有数据库条目带有空字符串字段:

将 Mongo Db 中的空字符串值替换为枚举之一后,错误消失了。