In my Java Play framework application, I want to store the ArrayList values in mongoDB.
I am having 3 values in the JSON which is loginid, phone, students; In that students is the ArrayList. I am storing the data in mongoDB like this:
{ "loginid" : "[email protected]", "phone" : "0123456789", "students" : [{"firstName" : "Jesse", "lastName" : "Varnell", "age" : "15", "gender" : "M" }, { "firstName" : "John", "lastName" : "Doe", "age" : "13", "gender" : "F"}] }
I am using mongo query to store the values like:
BasicDBObject searchQuery = new BasicDBObject();
BasicDBObject theUserObj = new BasicDBObject();
ArrayList<Student> student = new ArrayList<Student>();
if(studentArray != null && studentArray.size()>=0) {
Student stud = new Student();
for(int i = 0; i < studentArray.size(); i++){
stud = studentArray.get(i);
student.add(stud);
}
}
theUserObj.put("loginid", profile.loginid);
theUserObj.put("phone", profile.phone);
searchQuery.append("loginid", username);
theUserObj.put("students", student);
theUserCollection.update(searchQuery, theUserObj); //Got error on this line.
I am getting the following error:
Execution exception (In /app/controllers/StudentInfo.java around line 176)
IllegalArgumentException occured : can't serialize class models.Student
play.exceptions.JavaExecutionException: can't serialize class models.Student
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: can't serialize class models.Student
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:142)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:346)
at com.mongodb.DBCollection.update(DBCollection.java:165)
at com.mongodb.DBCollection.update(DBCollection.java:197)
at com.mongodb.DBCollection.update(DBCollection.java:209)
at controllers.StudentInfo.doStoreProfile(StudentInfo.java:176)
at controllers.StudentInfo.storeUserProfile(StudentInfo.java:212)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
How to store a value with ArrayList in MongoDB using Java?
Studentclass implementSerializable?