gpt4 book ai didi

java - MongoDB Java 驱动程序 @Discriminator @BsonDiscriminator 注解

转载 作者:行者123 更新时间:2023-12-01 17:53:18 25 4
gpt4 key购买 nike

Mongo 文档 http://mongodb.github.io/mongo-java-driver/3.11/bson/pojos/状态:

The easiest way to enable a discriminator is to annotate the abstract class with the Discriminator annotation.

但是我现在看到的问题是,3.11 驱动程序中没有 @Discriminator 注解。我不确定它是否与 @BsonDiscriminator 相同,我可以在 https://www.javadoc.io/static/org.mongodb/mongo-java-driver/3.11.0/org/bson/codecs/pojo/annotations/package-summary.html 找到它。

我应该如何使用@Discriminator以及它在哪里?

最佳答案

这似乎是 Mongo 文档中的一个错误。我使用了@BsonDiscriminator并且它有效。

我找不到好的例子,所以我在这里发布了我实现的内容来检查这一点。请注意,我使用了 jackson ,但这没有在这个答案中显示。因此,应删除 ParentPojo1Pojo2 上的一些注释:

@JsonTypeInfo(include=JsonTypeInfo.As.WRAPPER_OBJECT, use=JsonTypeInfo.Id.NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = Pojo1.class),
@JsonSubTypes.Type(value = Pojo2.class)})
@BsonDiscriminator
public abstract class Parent {

}

子类:

@Data
@NoArgsConstructor
public class Pojo1 extends Parent {
String string;
Integer number;
Boolean flag;
}

@Data
@NoArgsConstructor
public class Pojo2 extends Parent {
String string;
Integer number;
Boolean flag;
}

容器类:

@Data
@NoArgsConstructor
public class Container {
private String name;
private List<Parent> pojos;
}

ContainerDAO 持久保存并读取包含父对象集合的容器对象:

public class ContainerDAO {
private static final MongoDatabase DATABASE = MongoDB.getDatabase();
private static final MongoCollection<Container> CONTAINER_COLLECTION =
DATABASE.getCollection("containers", Container.class);

static {
CONTAINER_COLLECTION.createIndex(
Indexes.ascending("name"), new IndexOptions().unique(true));
}

public static void create(Container container){
Bson filter =
eq("name", container.getName());
ReplaceOptions options = new ReplaceOptions().upsert(true);
CONTAINER_COLLECTION.replaceOne(filter, container, options);
}

public static Container getByName(String name) {
Container container = CONTAINER_COLLECTION.find(
eq("name", name))
.first();
return container;
}

public static void deleteOne(String name) {
Bson filter =
eq("name", name);
CONTAINER_COLLECTION.deleteOne(filter);
}
}

以及数据库连接对象:

/**
* MongoDb database and connection settings class
*/
public class MongoDB {

private static final MongoDatabase DATABASE;

static {

...

PojoCodecProvider pojoCodecProvider = PojoCodecProvider
.builder().register(com.researchforgood.survey.jackson.Parent.class, com.researchforgood.survey.jackson.Pojo1.class, com.researchforgood.survey.jackson.Pojo2.class).build();

CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(pojoCodecProvider, PojoCodecProvider.builder().automatic(true).build()));
MongoClientSettings settings = MongoClientSettings.builder()
.codecRegistry(pojoCodecRegistry)
.applyConnectionString(new ConnectionString(settingsMap.get("url")))
.build();
MongoClient mongoClient = MongoClients.create(settings);
DATABASE = mongoClient.getDatabase(settingsMap.get("database"));
}

public static MongoDatabase getDatabase() {
return DATABASE;
}

}

这里是测试 - 使用示例

@Test
public void saveAndRestorePojo() throws JsonProcessingException {
ContainerDAO.create(container);

Container containerFromDB = ContainerDAO.getByName(container.getName());
LOG.info(containerFromDB.toString());

assertEquals(containerFromDB.getPojos().get(0).getClass(), Pojo1.class);
assertEquals(((Pojo1)containerFromDB.getPojos().get(0)).getString(), "Hello1!");
}

关于java - MongoDB Java 驱动程序 @Discriminator @BsonDiscriminator 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60760682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com