在我的Java Play框架应用程序中,我想将ArrayList值存储在mongoDB中。将数组ArrayList <Model>存储到Java中的MongoDB中
我在JSON中有3个值,它们是loginid,phone,students;在那里学生是ArrayList。我在MongoDB中存储的数据是这样的:
{ "loginid" : "[email protected]", "phone" : "", "students" : [{"firstName" : "Jesse", "lastName" : "Varnell", "age" : "15", "gender" : "M" }, { "firstName" : "John", "lastName" : "Doe", "age" : "13", "gender" : "F"}] }
我使用蒙戈查询存储喜欢的值:
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.
我收到以下错误:
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)
如何用ArrayList在MongoDB中使用Java存储值?
贵'Student'类实现'Serializable'? –
不是。它是Play in框架中的一个。 –
你可以编辑这门课吗?试着让它实现Serializable/ –