将平面DB文档映射到Morphia中的嵌套类

时间:2016-02-16 10:29:19

标签: java mongodb jackson morphia jongo

我正在移植一个Java应用程序,该应用程序曾经使用Jongo与MongoDB通信到Morphia +新的MongoDB Java驱动程序。

在数据库中有平面文档,如:

{
        "_id" : ObjectId("56c2e5b9b9b6e9753d4f11de"),
        "field1" : "foo",
        "field2" : "bar",
        "field3" : NumberLong("1455613369030"),
        "field4" : NumberLong("1455613369030"),
        "field5" : NumberLong(58),
        "field6" : NumberLong(1)
}

还有平面实体类将这些文档存储在Java应用程序中,使用jackson注释可以与Jongo一起使用:

public class MyPOJOClass {

    public static final String COLLECTION_NAME = "foobar";

    @JsonProperty(value = "_id")
    private String id;
    @JsonUnwrapped
    private FooBar foobar;
    @JsonUnwrapped
    private MyLongs longs;

    // Constructor and other methods

    public static class FooBar {
        @JsonProperty(value = "field1")
        private String foo;
        @JsonProperty(value = "field2")
        private String bar;
        // Consteructor and other methods…
    }

    public static class MyLongs {
        @JsonProperty(value = "field3")
        private long first;
        @JsonProperty(value = "field4")
        private long second;
        // etc…
    }
}

我能否以某种方式将此确切结构移植到Morphia,展平实体类或扩展文档(以便foobar字段位于一个嵌入文档中和另一份文件中的LongNumber字段)?

我没有找到@Embedded注释的任何例子(唯一一个看起来相关并给出一些希望的东西)来做这样的事情。我想最终得到类似的东西:

@Entity(MyPOJOClass.COLLECTION_NAME)
public class MyPOJOClass {

    public static final String COLLECTION_NAME = "foobar";

    @Id
    private ObjectId id;
    @Embedded(_SOME_MAGIC_?)
    private FooBar foobar;
    @Embedded(_SOME_MAGIC_?)
    private MyLongs longs;

    // Constructor and other methods

    @Embedded(_SOME_MAGIC_?)
    public static class FooBar {
        @Property("field1")
        private String foo;
        @Property("field2")
        private String bar;
        // Consteructor and other methods…
    }

    @Embedded(_SOME_MAGIC_?)
    public static class MyLongs {
        @Property("field3")
        private long first;
        @Property("field4")
        private long second;
        // etc…
    }
}

0 个答案:

没有答案