如何从Mongo数据源创建一个不可变的Java对象

huangapple go评论74阅读模式
英文:

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&#39;t work
MongoCollection&lt;Sample&gt; samples = db.getCollection(&quot;samples&quot;, 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注解:

https://mongodb.github.io/mongo-java-driver/3.5/bson/pojos/#supporting-pojos-without-no-args-constructors

@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:

https://mongodb.github.io/mongo-java-driver/3.5/bson/pojos/#supporting-pojos-without-no-args-constructors

<!-- language: java -->

@BsonDiscriminator
public final class Sample {

    private final String description;
    private final Integer min;
    private final Integer max;

    @BsonCreator
    public Sample(@BsonProperty(&quot;description&quot;) String description, @BsonProperty(&quot;min&quot;) Integer min, @BsonProperty(&quot;max&quot;) 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;
    }
}

huangapple
  • 本文由 发表于 2020年8月7日 00:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63287621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定