简体   繁体   English

Spring-data-mongo 无法使用构造函数实例化 java.util.List

[英]Spring-data-mongo unable to instantiate java.util.List using Constructor

Using spring-data-mongodb-1.5.4 and mongodb-driver-3.4.2使用spring-data-mongodb-1.5.4mongodb-driver-3.4.2

I've a class Hotel我有一个类Hotel

    public class Hotel {

        private String name;
        private int pricePerNight;
        private Address address;
        private List<Review> reviews;
//getter, setter, default constructor, parameterized constructor 

Review class : Review课:

public class Review {

    private int rating;
    private String description;
    private User user;
    private boolean isApproved;
 //getter, setter, default constructor, parameterized constructor 

When I am calling Aggregation.unwind("reviews");当我打电话给Aggregation.unwind("reviews"); it throws它抛出

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments org.springframework.data.mapping.model.MappingInstantiationException:无法使用带参数的构造函数 NO_CONSTRUCTOR 实例化 java.util.List

UnwindOperation unwindOperation = Aggregation.unwind("reviews");
Aggregation aggregation = Aggregation.newAggregation(unwindOperation);
AggregationResults<Hotel> results=mongoOperations.aggregate(aggregation,"hotel", Hotel.class);

I see this question but does't help me.我看到这个问题,但对我没有帮助。

How to resolve this?如何解决这个问题?

When you $unwind reviews field, query's return json structure does not match with your Hotel class anymore.当您$unwind reviews字段时,查询的返回 json 结构不再与您的Hotel类匹配。 Because $unwind operation makes reviews a sub object instead of a list.因为$unwind操作使reviews成为子对象而不是列表。 If you try your query in robomongo or some other tools, you can see your return object is like that如果您在 robomongo 或其他一些工具中尝试查询,您可以看到您的返回对象是这样的

{
  "_id" : ObjectId("59b519d72f9e340bcc830cb3"),
  "id" : "59b23c39c70ff63135f76b14",
  "name" : "Signature",
  "reviews" : {
    "id" : 1,
    "userName" : "Salman",
    "rating" : 8,
    "approved" : true
  }
}

So you should use another class instead of Hotel like UnwindedHotel所以你应该使用另一个类而不是像UnwindedHotel这样的Hotel

public class UnwindedHotel {

    private String name;
    private int pricePerNight;
    private Address address;
    private Review reviews;
}

UnwindOperation unwindOperation = Aggregation.unwind("reviews");
Aggregation aggregation = Aggregation.newAggregation(unwindOperation);
AggregationResults<UnwindedHotel> results=mongoOperations.aggregate(aggregation,"hotel", UnwindedHotel.class);

I had this problem and i solve it by using the "project" pipeline after unwind.我遇到了这个问题,我在放松后使用“项目”管道解决了这个问题。 in spring boot aggregation this will happen but in the mongo shell, it won't need a project after unwind在 spring boot 聚合中,这会发生,但在 mongo shell 中,展开后不需要项目

It occurs to me a very different problem making me crazy for long time a day ago and I think it could be very useful to share here even if it is not about unwind:一天前我突然想到一个非常不同的问题,让我发疯了很长时间,我认为在这里分享可能非常有用,即使它不是关于放松:

  • at the beginning I design a model with java + mongoDB in which I put some fields in a key inside a document and made several tests, for example {"header":{"key":"first", "value": 4}}一开始我用 java + mongoDB 设计了一个模型,其中我将一些字段放在文档内的键中并进行了几次测试,例如{"header":{"key":"first", "value": 4}}
  • then I changed my mind and think not to have simple nested objects but a list of them to be more general such as {"header":[{"key":"first", "value": 4}] }然后我改变了主意,认为不要有简单的嵌套对象,而是要有一个更通用的列表,例如{"header":[{"key":"first", "value": 4}] }

When I was working only with new documents all was running fine, but then making more general tests I continuously had crashes with this mongodb error about List because in my DB I had old documents without list but with simple objects I forgot to remove and which were in conflict with the new model wanting a list.当我只处理新文档时,一切都运行良好,但随后进行更一般的测试时,我不断因有关 List 的 mongodb 错误而崩溃,因为在我的数据库中,我有没有列表的旧文档,但有一些我忘记删除的简单对象,它们是与想要列表的新模型相冲突。

Simple had I to clean DB and starting tests from scratch.简单让我清理数据库并从头开始测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用带参数的构造函数NO_CONSTRUCTOR实例化java.util.List] - Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments ] “无法使用带有 arguments 的构造函数 NO_CONSTRUCTOR 实例化 java.util.List ”, - “Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments ”, 使用Spring-data在Java.util.List中插入$ currentDate - Insert $currentDate in java.util.List using Spring-data 是否可以使用 spring-data-mongo 1.10 创建 Mongo 视图? - Is it possible to create a Mongo view using spring-data-mongo 1.10? spring boot - 如何在HTTP控制器处理程序中避免“无法实例化[java.util.List]:指定的类是一个接口”? - spring boot - how to avoid “Failed to instantiate [java.util.List]: Specified class is an interface” in HTTP controller handler? Spring Mongodb-无法实例化 bean class [java.util.List]:指定 class 是一个接口 - Spring Mongodb- Could not instantiate bean class [java.util.List]: Specified class is an interface spring MVC3错误无法实例化bean类[java.util.List]:指定的类是偶尔的接口 - spring MVC3 errors Could not instantiate bean class [java.util.List]: Specified class is an interface Occasional Spring data elasticsearch:throws MappingInstantiationException: Failed to instantiate java.util.Set using constructor NO_CONSTRUCTOR with arguments - Spring data elasticsearch:throws MappingInstantiationException: Failed to instantiate java.util.Set using constructor NO_CONSTRUCTOR with arguments Hibernate 无法实例化抽象类或接口:java.util.List - Hibernate Cannot Instantiate Abstract class or interface: java.util.List 无法将java.util.List实例化为JavaBeans组件 - Cannot instantiate java.util.List as a JavaBeans component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM