更新文档中的对象列表时出现 CodecConfigurationException

CodecConfigurationException when updating list of objects in document

我目前遇到特定实体的 CodecConfigurationException。我已经尝试为有问题的对象添加自定义编解码器,但这似乎没有解决问题。错误信息是:

org.bson.codecs.configuration.CodecConfiguration: Can't find a codec for class net.fancycow.common.ecs.entities.FancyBox

我觉得奇怪的是 PlayerData 有一个名为 Unlock 的另一个自定义 class(我已从代码中省略)的列表,它从未抱怨过 class 编解码器,但对于某些FancyBox 这样做的原因或其他原因。 Entityclass中看到的Componentclass只是我各种组件实现的一个接口,所以FancyBox会有各种不同的组件类型。如果有人能指出我如何解决这个问题的正确方向,我将非常感激,因为我还没有找到适合我的案例的解决方案。

PlayerData.class

@NoArgsConstructor
@Entity(value = "players", noClassnameStored = true)
public class PlayerData {
    @Id
    @Getter
    private String uuid;

    @Getter
    private List<FancyBox> fancyBoxes = Lists.newArrayList();

    public PlayerData(UUID uuid) {
        this.uuid = uuid.toString();
    }
}

FancyBox.class

@NoArgsConstructor
public class FancyBox extends Entity {
    /**
     * Minimum quality of fancy boxes.
     */
    public static final int MIN_QUALITY = 1;
    /**
     * Maximum quality of fancy boxes.
     */
    public static final int MAX_QUALITY = 5;
    /**
     * Rarity count mapping for each quality.
     */
    public static final Multimap<Integer, Entry<Rarity, Integer>> QUALITY_GENERATION = ArrayListMultimap.create();

    static {
        QUALITY_GENERATION.putAll(1, Lists.newArrayList(new SimpleEntry<>(Rarity.COMMON, 3),
                new SimpleEntry<>(Rarity.UNCOMMON, 2),
                new SimpleEntry<>(Rarity.RARE, 1)));
        QUALITY_GENERATION.putAll(2, Lists.newArrayList(new SimpleEntry<>(Rarity.COMMON, 2),
                new SimpleEntry<>(Rarity.UNCOMMON, 2),
                new SimpleEntry<>(Rarity.RARE, 1),
                new SimpleEntry<>(Rarity.EPIC, 1)));
        QUALITY_GENERATION.putAll(3, Lists.newArrayList(new SimpleEntry<>(Rarity.UNCOMMON, 2),
                new SimpleEntry<>(Rarity.RARE, 2),
                new SimpleEntry<>(Rarity.EPIC, 1),
                new SimpleEntry<>(Rarity.LEGENDARY, 1)));
        QUALITY_GENERATION.putAll(4, Lists.newArrayList(new SimpleEntry<>(Rarity.RARE, 2),
                new SimpleEntry<>(Rarity.EPIC, 2),
                new SimpleEntry<>(Rarity.LEGENDARY, 1),
                new SimpleEntry<>(Rarity.SUPREME, 1)));
        QUALITY_GENERATION.putAll(1, Lists.newArrayList(new SimpleEntry<>(Rarity.EPIC, 3),
                new SimpleEntry<>(Rarity.LEGENDARY, 2),
                new SimpleEntry<>(Rarity.SUPREME, 1)));
    }

    public FancyBox(@NonNull FancyBoxType type, int quality) {
        addComponent(new FancyBoxTypeComponent(type));
        addComponent(new QualityComponent(quality, MAX_QUALITY));
        addComponent(new DateCreationComponent());
    }
}

Entity.class

public class Entity {
    protected List<Component> components = Lists.newArrayList();

    public Entity addComponent(@NonNull Component component) {
        if (!hasComponent(component.getClass()))
            this.components.add(component);
        return this;
    }

    public Entity removeComponent(@NonNull Component component) {
        this.components.remove(component);
        return this;
    }

    public Entity removeComponent(@NonNull Class<? extends Component> type) {
        Lists.newArrayList(this.components).stream()
                .filter(component -> component.getClass() == type)
                .forEach(component -> this.components.remove(component));
        return this;
    }

    public <T extends Component> T getComponent(@NonNull Class<T> type) {
        return (T) this.components.stream()
                .filter(component -> type.isAssignableFrom(component.getClass()))
                .findFirst()
                .orElse(null);
    }

    public boolean hasComponent(@NonNull Class<?> type) {
        return this.components.stream()
                .filter(component -> component.getClass().equals(type))
                .count() > 0;
    }
}

所以经过进一步的测试和研究,我发现这是 Morphia 1.2.x 中的 UpdateOperations#addAll 的问题。我更新到 Morphia 1.3.0-SNAPSHOT 并切换到 UpdateOperation#push 现在一切正常。