[英]Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments ]
[英]Spring-data-mongo unable to instantiate java.util.List using Constructor
使用spring-data-mongodb-1.5.4
和mongodb-driver-3.4.2
我有一個類Hotel
public class Hotel {
private String name;
private int pricePerNight;
private Address address;
private List<Review> reviews;
//getter, setter, default constructor, parameterized constructor
Review
課:
public class Review {
private int rating;
private String description;
private User user;
private boolean isApproved;
//getter, setter, default constructor, parameterized constructor
當我打電話給Aggregation.unwind("reviews");
它拋出
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);
我看到這個問題,但對我沒有幫助。
如何解決這個問題?
當您$unwind
reviews
字段時,查詢的返回 json 結構不再與您的Hotel
類匹配。 因為$unwind
操作使reviews
成為子對象而不是列表。 如果您在 robomongo 或其他一些工具中嘗試查詢,您可以看到您的返回對象是這樣的
{
"_id" : ObjectId("59b519d72f9e340bcc830cb3"),
"id" : "59b23c39c70ff63135f76b14",
"name" : "Signature",
"reviews" : {
"id" : 1,
"userName" : "Salman",
"rating" : 8,
"approved" : true
}
}
所以你應該使用另一個類而不是像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);
我遇到了這個問題,我在放松后使用“項目”管道解決了這個問題。 在 spring boot 聚合中,這會發生,但在 mongo shell 中,展開后不需要項目
一天前我突然想到一個非常不同的問題,讓我發瘋了很長時間,我認為在這里分享可能非常有用,即使它不是關於放松:
{"header":{"key":"first", "value": 4}}
{"header":[{"key":"first", "value": 4}] }
當我只處理新文檔時,一切都運行良好,但隨后進行更一般的測試時,我不斷因有關 List 的 mongodb 錯誤而崩潰,因為在我的數據庫中,我有沒有列表的舊文檔,但有一些我忘記刪除的簡單對象,它們是與想要列表的新模型相沖突。
簡單讓我清理數據庫並從頭開始測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.