[英]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.4
和mongodb-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:一天前我突然想到一个非常不同的问题,让我发疯了很长时间,我认为在这里分享可能非常有用,即使它不是关于放松:
{"header":{"key":"first", "value": 4}}
{"header":{"key":"first", "value": 4}}
{"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.