更新文档

时间:2016-09-19 01:57:43

标签: mongodb morphia

我目前正在遇到一个特定实体的CodecConfigurationException。我尝试为相关对象添加自定义编解码器,但似乎没有解决问题。错误消息是:

  

org.bson.codecs.configuration.CodecConfiguration:无法找到类net.fancycow.common.ecs.entities.FancyBox的编解码器

我觉得奇怪的是,PlayerData有一个名为Unlock的另一个自定义类(我已经从代码中省略)的列表,并且它从不抱怨类编解码器,但由于某种原因它对FancyBox这样做。在Entity类中看到的Component类只是我的各种组件实现的接口,因此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;
    }
}

1 个答案:

答案 0 :(得分:1)

因此,经过进一步的测试和研究,我发现Morphia 1.2.x中的UpdateOperations#addAll存在问题。我更新到Morphia 1.3.0-SNAPSHOT并切换到UpdateOperation#push,现在一切都很好。