2014-01-30 32 views
1

在我的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存储值?

+0

贵'Student'类实现'Serializable'? –

+0

不是。它是Play in框架中的一个。 –

+0

你可以编辑这门课吗?试着让它实现Serializable/ –

回答

4

DBObject和实现类只能处理BSON类型,其中你不是Student类。

有很多框架可以映射|将域对象(如Student)转换为|来自BSON的代表和文件。由于您没有明显使用,您必须手动将每个Student自己转换为DBObject

BasicDBObject searchQuery = new BasicDBObject("loginid", username); 
BasicDBObject theUserObj = new BasicDBObject(); 

List<Object> studentsDBList = new BasicDBList(); 

for (Student student : studentArray) { 
    DBObject studentDBObject = new BasicDBObject(); 
    studentDBObject.put("firstName", student.getFirstName()); 
    studentDBObject.put("lastName", student.getLastName()); 
    studentDBObject.put("age", student.getAge()); 
    studentDBObject.put("gender", student.getGender()); 
    studentsDBList.add(studentDBObject); 
} 

theUserObj.put("phone", profile.phone); 
theUserObj.put("students", studentsDBList); 

theUserCollection.update(searchQuery, theUserObj); 

另外请注意,你不必把loginidtheUserObj,因为你只查询它

+0

你好,你怎么能做到这一点?获取MongoDb值作为一个数组列表 – Jesse

+0

按照上面的例子:'List aList =(List )theUserObj.get(“students”)'。 'get'方法返回一个'BasicDBList',它继承自'ArrayList' –

0

您正试图在MongoDB中添加一个普通的JAVA对象。

ArrayList<Student> student = new ArrayList<Student>(); 
theUserObj.put("students", student); 

在上面的stmt中,你试图把学生ArrayList对象放到MongoDB数组中。因此,Mongo为什么会抛出可序列化的错误。

您应该使用DBObject初始化您的数组,并尝试在您的代码中使用它们以避免此错误。

List<DBObject> students= new ArrayList<>(); 

并尝试在此对象中添加学生的详细信息。

或U还可以使用BasicDBList如下

for(Tag tag:tags){ 
    BasicDBList student= new BasicDBList(); 
    for(int i = 0; i < studentArray.size(); i++){ 
    stud = studentArray.get(i); 
    student.add(stud); 
} 

上面可能含有一些语法错误,但这些类型初始化的应该有助于通过JAVA

到里面添加蒙戈值数组
+0

'stud = studentArray.get(i); student.add(stud);'这不会工作,因为'Stundent'对象仍然被添加到列表中 –

+0

是的..代码可能包含一些错误。我没有执行和检查。但是我试图暗示你不能将JAVA对象插入Mongo – arunb2w

相关问题