java - 使用泛型为 MongoDB POJO 创建包装器
问题描述
更新2: 我想我已经取得了一些进展。我的 IDE 对我的代码更满意,但我只有最后一行不高兴:
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientURI connectionString = new MongoClientURI("my-mongo-string");
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("New");
database = database.withCodecRegistry(pojoCodecRegistry);
MongoCollection<? extends Person> collection = database.getCollection("people", this.getClass());
collection = collection.withCodecRegistry(pojoCodecRegistry);
collection.insertOne(this);
我已经将我的“集合”的名称换成了“人”,以暂时防止与 Java 的“集合”发生冲突。除了最后一行,一切似乎都很好,这给了我这个错误:insertOne (capture<? extends Utils.Person>) in MongoCollection cannot be applied to (Utils.Person)
. 'this' 指的是我的 person 类。我在这里做错了什么?
更新:下面的原始问题。我相信我现在已经确定仿制药是我最好的选择。但是,我在“生成”这两行时遇到了一些问题(假设我已正确初始化数据库):
MongoCollection<MyClass> collection = database.getCollection("SomeString", MyClass.class);
做这个的最好方式是什么?Java 似乎不喜欢在泛型上使用 .class。
原始问题:
所以我想利用 MongoDB 驱动程序存储 PJOS 的能力。示例在这里。但是我计划将多个类存储到数据库中,并且我不想每次都重新键入所有连接代码,也不想复制和粘贴然后对每个类的代码进行细微修改。我有创建一个“集合”类的想法,然后扩展它,但它并没有按照我的预期工作。这是我的代码:
public class Collection {
private MongoCollection<Collection> collection;
public Collection(){}
public Collection(String databaseName){
databaseName = databaseName.toLowerCase().replaceAll(" ", "");
CodecRegistry pojoCodecRegistry = fromRegistries(com.mongodb.MongoClient.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientURI connectionString = new MongoClientURI("my-mongo-connection-string");
com.mongodb.MongoClient mongoClient = new com.mongodb.MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase(databaseName);
database = database.withCodecRegistry(pojoCodecRegistry);
collection = database.getCollection(this.getClass().getName(), Collection.class);
}
public Collection findOne(Document document){
return collection.find(document).first();
}
public void save(){
collection.insertOne(this);
}
}
如您所见,我希望能够将数据库名称传递给从此类继承的任何类(或任何正确的做法。),并让我的代码连接到数据库并查找或保存 POJO相应的子类正确。例如,如果我有一个带有电话号码的 Person 类,我可以扩展我的 Collections 类并依靠一切工作。现在,当我在子类上调用 save 函数时,它会尝试保存父 Collection,但出现错误Can't find a codec for class Utils.MongoDb.Collection.
(我的 Collection 类在我自己的 Utils.MongoDb 命名空间中)。我只是错过了什么吗?有没有更好的方法来解决这个问题?我认为让它工作的唯一方法是将我的 Collection 构造函数中的代码复制并粘贴到每个类中,并使用正确的类变量对其进行修改。我希望这一切都清楚。提前致谢!
解决方案
啊哈!我懂了!我必须做的是生成并编写一个辅助方法,这样我就可以将“this”作为对象传入。我还必须对通用 T 进行强制转换。我知道这是未经检查的强制转换,但我不太确定如何在此处对其进行检查。无论哪种方式,它都有效!
public <T> void saveObj(T obj){
String dbName = "New";
MongoDatabase database = getMongoDatabase(dbName);
MongoCollection<T> collection = (MongoCollection<T>) database.getCollection(obj.getClass().getSimpleName(), obj.getClass());
collection.insertOne(obj);
}
public MongoDatabase getMongoDatabase(String dbName) {
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientURI connectionString = new MongoClientURI("my-mongo-string");
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase(dbName);
database = database.withCodecRegistry(pojoCodecRegistry);
return database;
}
推荐阅读
- javascript - 我在 MySQL Hosting 中创建的数据库正在形成一个表,其中包含来自 JavaScript 中的 API 的空数据
- java - 如何以编程方式检测 java.lang.OutOfMemoryError: Direct buffer memory to create a java heap dump?
- python - python list问题-为什么函数会产生淡蓝色?-vscode
- amazon-web-services - ACL 配置
- python - 偏斜正态分布(偏斜与 alpha 或参数) - 生成 N 个整数(Python SciPy)
- swift - 如何解决 Xcode 非法指令:4 错误?
- javascript - 如何将 video.removeEventListener 添加到代码中
- jquery - 请求参数为空,在提交带有图像的表单数据 AJAX 后
- c++ - 初始化自定义类 C++ 的二维数组
- ajax - 抛出异常:大文件上传 ASP.Net 核心上 System.Private.CoreLib.dll 中的“System.IO.InvalidDataException”