Mongo Docs http://mongodb.github.io/mongo-java-driver/3.11/bson/pojos/指出:
启用区分符的最简单方法是对摘要进行注释
具有Discriminator注释的类。
但是我现在看到的问题是,在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,它有效。
我找不到很好的例子,所以我在这里发布了我为检查这一点而实施的内容。请注意,我使用了杰克逊,但此答案中未显示。因此,应在Parent,Pojo1,Pojo2上删除一些注释:
@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;
}
容器类
@数据
@NoArgsConstructor
公共类容器{
私有字符串名称;
私人名单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/