1

我正在使用 MongoDB 构建一个 Spring MVC 应用程序。我如何在春天从 mongo 读取矩阵?我有一个模型使用 MongoTemplate 类可以很好地坚持到 mongo:

Matrix m = new Matrix();
m.setId(UUID.randomUUID().toString());
m.setValues(values);
mongoTemplate.insert(m, "matrix");

上面的代码工作得很好。Values 是一个 double[][] 并且它是持久的。我正在使用 MongoRepository 类的扩展来对矩阵列表进行 findAll() 调用。

public interface MatrixRepository extends MongoRepository<Matrix, String> {

    Matrix findById(String id);
}

在我的服务类中:

public List<Matrix> readAll() {
    return matrixRepository.findAll();
}

调用 this 会导致以下堆栈跟踪:

org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [java.lang.Double]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Double.<init>()
    org.springframework.data.mapping.model.BeanWrapper.<init>(BeanWrapper.java:105)
    org.springframework.data.mapping.model.BeanWrapper.create(BeanWrapper.java:73)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:239)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:187)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:736)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:695)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter$2.doWithPersistentProperty(MappingMongoConverter.java:252)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter$2.doWithPersistentProperty(MappingMongoConverter.java:242)
    org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:173)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:242)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:187)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:151)
    org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:73)
    org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1693)
    org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1444)
    org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1259)
    org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1248)
    org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:471)
    org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:255)
    org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:192)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
4

1 回答 1

1

如果您的 monogo 实体类在 spring data mongodb 1.0.0.M5 环境中具有 Double 或 double 字段,则会发生这种情况。您可以通过在 pom.xml 中用更新版本 1.3.3 RELEASE 替换您的 spring 数据 mongodb 来解决此问题

于 2014-03-07T09:27:11.783 回答