簡體   English   中英

Morphia無法映射整數列表

[英]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;
    }

}

如果您查看此測試 ,則可以看到List<List<Integer>>正常工作。 查看您的示例,我看不到任何明顯的錯誤會導致您得到錯誤,但是我至少可以驗證List<List<Integer>>有效。 您正在使用哪個版本的Java驅動程序? 至少應在3.x行中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM