Spring-data-mongo unable to instantiate java.util.List using Constructor -
using spring-data-mongodb-1.5.4
, mongodb-driver-3.4.2
i've class 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 :
public class review { private int rating; private string description; private user user; private boolean isapproved; //getter, setter, default constructor, parameterized constructor
when calling aggregation.unwind("reviews");
throws
org.springframework.data.mapping.model.mappinginstantiationexception: failed instantiate java.util.list using constructor no_constructor arguments
unwindoperation unwindoperation = aggregation.unwind("reviews"); aggregation aggregation = aggregation.newaggregation(unwindoperation); aggregationresults<hotel> results=mongooperations.aggregate(aggregation,"hotel", hotel.class);
i see this question does't me.
how resolve this?
when $unwind
reviews
field, query's return json structure not match hotel
class anymore. because $unwind
operation makes reviews
sub object instead of list. if try query in robomongo or other tools, can see return object
{ "_id" : objectid("59b519d72f9e340bcc830cb3"), "id" : "59b23c39c70ff63135f76b14", "name" : "signature", "reviews" : { "id" : 1, "username" : "salman", "rating" : 8, "approved" : true } }
so should use class instead of hotel
like unwindedhotel
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);
Comments
Post a Comment