使用 Java 将 Java 对象插入 MongoDB 集合

2022-09-01 06:20:28

我正在尝试使用Java将整个Java对象插入MongoDB集合中。我收到以下错误:

错误:

Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class net.yogesh.test.Employee
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:185)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:119)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:65)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:176)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:134)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:129)
    at com.mongodb.DBCollection.save(DBCollection.java:418)
    at net.yogesh.test.test.main(test.java:31)

Emplyoee.java (POJO)

package net.yogesh.test;

import java.io.Serializable;

public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;
    private long no;
    private String name;

    public Employee() {
    }

    public long getNo() {
        return no;
    }

    public void setNo(long no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

主要方法类(测试.java)

package net.yogesh.test;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class test {

    public static void main(String[] args) throws UnknownHostException,
            MongoException {

        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("test");

        Employee employee = new Employee();
        employee.setNo(1L);
        employee.setName("yogesh");


        BasicDBObject basicDBObject = new BasicDBObject("Name", employee);

        DBCollection dbCollection = db.getCollection("NameColl");

        dbCollection.save(basicDBObject);   

    }

}

任何人都可以解释为什么我收到这个错误吗?


答案 1

我有点困惑,不知道为什么你会认为这首先会起作用。您需要知道的第一件事是如何将POJO映射到MongoDB文档。目前,您没有告诉系统(您的代码)如何执行此操作。

您可以为此使用映射库(想到Morphia)或使用ReflemationDBObject。任何一种解决方案都允许您将POJO映射到MongoDB文档或MongoDB文档映射到POJO(前一种方式比后者好得多)。


答案 2
DB db = mongoClient.getDB( "mydb" );

coll = db.getCollection("testCollection");

Employee emp = new Employee();
emp.setId("1001");
emp.setName("John Doe");

//Converting a custom Class(Employee) to BasicDBObject
Gson gson = new Gson();
BasicDBObject obj = (BasicDBObject)JSON.parse(gson.toJson(emp));
coll.insert(obj);
findEmployee(new BasicDBObject("id","1001"));


public static void findEmployee(BasicDBObject query){

    DBCursor cursor = coll.find(query);

    try {
       while(cursor.hasNext()) {
          DBObject dbobj = cursor.next();
        //Converting BasicDBObject to a custom Class(Employee)
          Employee emp = (new Gson()).fromJson(dbobj.toString(), Employee.class);
          System.out.println(emp.getName());
       }
    } finally {
       cursor.close();
    }

}

我认为发布双向转换的代码会很有用。
存储员工对象
查找并重新创建员工对象
希望这很有用。