【发布时间】:2018-11-26 13:04:34
【问题描述】:
我最近开始使用 Java 和 MongoDB,发现事情不像 C# 那样简单。
在 C# 中,我可以使用以下行创建一个类(作为模型)以将其保存为 MongoDB 中的 Bson 对象。
acc = db.GetCollection<AccountModel>("accounts");
在 Java 中,我得到了这样的类:
accs = db.getCollection("accounts", AccountModel.class);
当我尝试插入这个 Bson 对象时,我会像这样填充它
public void InsertPlayer(String username){
Model_Account newAccount = new Model_Account();
newAccount.Username = "username";
newAccount.Password = "password";
newAccount.Email = "email@hotmail.com";
accounts.insertOne(newAccount);
}
与我在 C# 中的操作方式非常相似,但在 Java 中出现此错误:
Caused by:org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class AccountModel.
据我了解,我需要 POJO 编解码器来实现相同的功能,这是正确的吗?如果是,我该如何创建它?
提前谢谢你!
【问题讨论】:
标签: java c# mongodb codec bson