5

my project is using Spring data mongodb. I was not having below error until i made an edit to one of the document that has a field with Array of Documents in it. It was working fine before but now I keep getting the below error.

The field i updated was impapps in the Projects POJO class. I am not sure how to clear this error tried different things but did not work out.

    SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mongodproject] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:60)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:232)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:212)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1008)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$100(MappingMongoConverter.java:75)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:957)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:713)

Here are my POJO and Spring Repository class.

Project POJO Class

@Document(collection="releases")
 public class Project {
@Id
private String id;
 ......
@Field("impapps")
private List<ImpactedApplications> impapps=new ArrayList<ImpactedApplications>();
    .....getters/setters
}   

ImpactedApplication POJO Class:

public class ImpactedApplications {

@Field("appid")
private String appId;
.....
@Field("repository")
private List<ScriptsRepo> rep=new ArrayList<ScriptsRepo>();
@Field("artifacts")
private List<Artifacts> artifacts=new ArrayList<Artifacts>();
     //getter and setters

}

Artifacts POJO Class

public class Artifacts {

    @Field("artifacttype")
    private String artifactType;
    @Field("documentlink")
    private String documentLink;
    @Field("arttestphase")
    private String artTestPhase;
    @Field("artifactname")
    private ArtifactsEnums artifactsNames;
    @Field("startdate")
    private String startDate;
    @Field("enddate")
    private String endDate;
    @Field("peerrev")
    private boolean peerReview;
    @Field("busrev")
    private boolean busReview;
    @Field("na")
    private boolean na;

Spring Repository classes

public interface ProjectRepository extends Repository<Project, String> {

Project findById(String id);
List<Project> findByYearAndReleaseMonthNoOrderByProjectNameDesc(String year,String month, Sort sort);
Project findByYearAndReleaseMonthNoAndId(String year, String month,String id);

Whenever i call the above methods i keep getting the exception.

Below is how my document is looking currently.

enter image description here

6
  • 1
    What do the documents look like you're trying to read? Can you check, that the values of impapps, repository and artifacts are arrays in MongoDB? If so, the read(…) method should run into the typeToUse.isCollectionLike() && dbo instanceof BasicDBList clause and properly create a collection for the property. I am assuming you rather find something not an array and thus the entity resolution kicks in. PS: Would you mind properly formatting the code samples? Makes the post much better to read and will probably attract more people to answer. Commented Dec 19, 2013 at 7:19
  • @OliverGierke Thanks for your reply. I have formatted my question a bit. And i have the document pic attached. impappa is created as an array, artifacts is created as array and repository is currently created as blank array currently. Commented Dec 19, 2013 at 15:53
  • now when i see it close..i see that impapps is storing as an document instead of an array of documents..I think there is issues in my code. When i edit the impapps looks like it is not storing as an array but as a document. Commented Dec 19, 2013 at 16:57
  • @OliverGierke Since you are here..can you please tell me what is the right way to update an exiting document in an array..I tried this but it is just replacing it as document. Criteria crit=new Criteria().andOperator( Criteria.where("_id").is(projectId), Criteria.where("impapps.appid").is(impapp.getAppId()) );Query query=new Query(crit);Update upd=new Update(); upd.set("impapps", impapp);mongoTemplate.updateFirst(query, upd, Project.class) Commented Dec 19, 2013 at 18:36
  • @OliverGierke nevermind sir. I got it by adding a $ sign. Thank you for looking into the post. You have great holidays!! Commented Dec 19, 2013 at 18:44

3 Answers 3

4

The impapps field in your document is not an array but a nested document. So if you change your List<ImpactedApplications> to a simple ImpactedApplications this should read fine.

Sign up to request clarification or add additional context in comments.

Comments

2

I have got the same exception :

Try to declare your field like this :

@Field("impapps")
ArrayList<ImpactedApplications> impapps = new ArrayList<ImpactedApplications>();

I don't like to do that but it works for me.

edit:

My issue was due to unwind operation during aggregation. It transform array (declared as List<> in my class) into object and then reflection doesn't work because spring was expecting a list.

Comments

0

use ArrayList someObjects instead of List someObjects.

It worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.