Morphia 无法映射 List of List of Integer

Morphia can't map List of List of Integer

我有一个我正在处理的问题的简化示例。我正在尝试加载包含数字数组的文档。

{
    "_id" : ObjectId("570cf0167640ed9f8bcff8e7"),
    "matrix" : [ 
        [ 
            42
        ]
    ]
}

为了创建所有索引,我调用 org.mongodb.morphia.Datastore#ensureIndexes()。据我了解,根据记录,我需要调用 org.mongodb.morphia.Morphia#map(Class ...) 来告诉 Morphia 在哪个 类 上寻找 @Indexes 注释。如果没有 Morphia#map(...),应用程序运行良好(并且不会像预期的那样创建索引)。如果我添加 Morphia#map(...) 我会得到一个异常。

Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [elementData]
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:74)
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:772)
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:230)
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:191)
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134)
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146)
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117)
    at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:150)
    at it.test.Main.fails(Main.java:41)
    at it.test.Main.main(Main.java:24)
Caused by: java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [elementData]
    at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:168)
    at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:160)
    at org.bson.types.BasicBSONList.get(BasicBSONList.java:105)
    at org.mongodb.morphia.mapping.MappedField.getDbObjectValue(MappedField.java:190)
    at org.mongodb.morphia.converters.Converters.fromDBObject(Converters.java:121)
    at org.mongodb.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:20)
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:766)
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:230)
    at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:206)
    at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:142)
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:45)
    ... 9 more

有人可以解释为什么显式调用 map() 会破坏 Morphia 吗?

以下示例重现了该问题(只需添加 org.mongodb.morphia:morphia:1.1.1 作为依赖)。

package it.test;

import java.util.List;

import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.dao.BasicDAO;
import org.mongodb.morphia.query.QueryResults;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

import it.test.model.Doc;

public class Main {
    private static final String URI = "mongodb://localhost:27017";
    private static final String NAME = "test";
    private static final MongoClientURI CLIENT_URI = new MongoClientURI(URI + "/" + NAME);

    public static void main(String[] args) {
        Main main = new Main();

        main.works();
        main.fails();
    }

    private void fails() {
        try (MongoClient client = new MongoClient(CLIENT_URI)) {
            Morphia morphia = new Morphia();
            morphia.getMapper().getOptions().setStoreEmpties(true);
            morphia.mapPackage("it.test.model");

            find(morphia, client, CLIENT_URI.getDatabase());
        }
    }

    private void works() {
        try (MongoClient client = new MongoClient(CLIENT_URI)) {
            Morphia morphia = new Morphia();
            morphia.getMapper().getOptions().setStoreEmpties(true);
            // morphia.mapPackage("it.test.model"); // bad call?

            find(morphia, client, CLIENT_URI.getDatabase());
        }
    }

    private void find(Morphia morphia, MongoClient client, String dbName) {
        Datastore datastore = morphia.createDatastore(client, dbName);

        BasicDAO<Doc, ObjectId> dao = new BasicDAO<>(Doc.class, datastore);
        QueryResults<Doc> result = dao.find();
        List<Doc> rootEntities = result.asList();

        System.out.println("Found " + rootEntities.size() + " RootEntity documents.");
    }
}

package it.test.model;

import java.util.List;

import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;

@Entity
public class Doc {

    @Id
    private ObjectId id;

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    private List<List<Integer>> matrix;

    public List<List<Integer>> getMatrix() {
        return matrix;
    }

    public void setMatrix(List<List<Integer>> matrix) {
        this.matrix = matrix;
    }

}

如果您查看 this test,您会发现 List<List<Integer>> 工作得很好。看看你的例子,我没有看到任何明显的东西会导致你得到的错误,但我至少可以验证 List<List<Integer>> 是否有效。您使用的是什么版本的 Java 驱动程序?它至少应该在 3.x 行。