英文:
How to create an immutable Java Object from a Mongo datasource
问题
I'm looking to create immutable Java objects directly from a Mongo collection, for example:
public final class Sample {
private final String description;
private final Integer min;
private final Integer max;
public Sample(String description, Integer min, Integer max) {
this.description = description;
this.min = min;
this.max = max;
}
public String getDescription() {
return description;
}
public Integer getMin() {
return min;
}
public Integer getMax() {
return max;
}
}
I've been using the PojoCodecProvider
to persist the objects, which works well. However, I obviously can't use this for loading the object back since there is no no-arg constructor:
// Doesn't work
MongoCollection<Sample> samples = db.getCollection("samples", Sample.class);
Is there a way to do this without extracting all the attributes from the Document
and calling the constructor myself? There are going to be lots of these types of objects, and some will be quite large. If there's a convenient way to do this, it would be a great help!
Many thanks.
英文:
I'm looking to create immutable Java objects directly from a Mongo collection, for example:
public final class Sample {
private final String description;
private final Integer min;
private final Integer max;
public Sample(String description, Integer min, Integer max) {
this.description = description;
this.min = min;
this.max = max;
}
public String getDescription() {
return description;
}
public Integer getMin() {
return min;
}
public Integer getMax() {
return max;
}
}
I've been using the PojoCodecProvider
to persist the objects, which works well. However I obviously can't use this for loading the object back, since there is no no-arg constructor:
//Doesn't work
MongoCollection<Sample> samples = db.getCollection("samples", Sample.class);
Is there a way to do this without extracting all the attributes from the Document
and calling the constructor myself? There are going to be lots of these types of object and some will be quite large, if there's a convenient way to do this it would be a great help!
Many thanks.
答案1
得分: 1
您可以按照MongoDB文档中的说明向您的类添加BSON注解:
@BsonDiscriminator
public final class Sample {
private final String description;
private final Integer min;
private final Integer max;
@BsonCreator
public Sample(@BsonProperty("description") String description, @BsonProperty("min") Integer min, @BsonProperty("max") Integer max) {
this.description = description;
this.min = min;
this.max = max;
}
public String getDescription() {
return description;
}
public Integer getMin() {
return min;
}
public Integer getMax() {
return max;
}
}
英文:
You can add BSON annotations to your class as stated in mongoDB documentation:
<!-- language: java -->
@BsonDiscriminator
public final class Sample {
private final String description;
private final Integer min;
private final Integer max;
@BsonCreator
public Sample(@BsonProperty("description") String description, @BsonProperty("min") Integer min, @BsonProperty("max") Integer max) {
this.description = description;
this.min = min;
this.max = max;
}
public String getDescription() {
return description;
}
public Integer getMin() {
return min;
}
public Integer getMax() {
return max;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论