Truerick
Truerick

Reputation: 99

Java Spring + MongoTemplate cannot retrieve List of Object

I had a Java Class linked to a MongoDB Collection:

@Document(collection = "my_collection")
public class Ev extends MyDTO{
    @Id
    private String id;
    @Indexed
    private String sessionId;
    private List<String> findings;
}

I had to change findings in this

    private List<MyObject> findings;

Declared as

public class MyObject {
    private String find;
    private String description;
    private int number;
    private List<SecondaryObj> details;
}

Here are the constructors

public MyObject(String find, int number) {
    super();
    this.find= find;
    this.number= number;
}

public MyObject(String find, int number, List<SecondaryObj> details) {
    super();
    this.find= find;
    this.details = details;
    this.number= number;
}

So in mongoDB I have a situation similar to

{
    "_id" : ObjectId("5b487a2667a1aa18f*******"),
    "sessionId" : "abc123mySessionId",
    "findings" : [ 
        {
            "find" : "HTTPS",
            "description" : "I found HTTPS",
            "number" : 10,
            "details": [
                {"a":"1", "b":"2"},
                {"a":"2", "b":"3"}
            ]
        },
        {
            "find" : "NAME",
            "description" : "I found name",
            "number" : 3,
            "details": [
                {"a":"1", "b":"2"},
                {"a":"2", "b":"3"}
            ]
        }
    ]
}

I obviously updated all the methods to match the new data set, but if I try to retrieve

Query searchQuery = new Query(Criteria.where("sessionId").is("abc123mySessionId"));
Ev result = mongoTemplate.findOne(searchQuery, Ev.class);

I obtain this error

Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.my.project.domain.MyObject using constructor NO_CONSTRUCTOR with arguments

with root cause

java.lang.NoSuchMethodException: om.my.project.domain.MyObject.<init>()

I'm using spring-data-mongodb version 2.0.8 and mongo-java-driver version 3.8.0

I think I should declare MyObject somewhere, but I'm pretty new in Spring, so I'm trying in a kinda blind way... Any suggestion?

Upvotes: 0

Views: 1500

Answers (2)

yuppie-flu
yuppie-flu

Reputation: 1480

You have two non-zero-argument constructors and Spring does not know which one to call. It tries to call no-args constructor, but your class does not have that one. Check Spring Data Mongo docs

You can create no-args constructor and mark it with @PersistenceConstructor annotation. This way Spring calls it to create an object and sets fields via reflection based on a document fields names, so no setters are required.

Upvotes: 2

Naresh N. Banda
Naresh N. Banda

Reputation: 1

@Document(collection = "my_collection")
public class Ev extends MyDTO{
@Id
private String id;
@Indexed
private String sessionId;
private List<MyObject> findings;}

public class MyObject {
private String find;
private String description;
private int number;}

In this it work fine for me in spring-boot-starter-data-mongodb - version 2.0.3.RELEASE

Upvotes: 0

Related Questions