I am porting a Java application that used to use Jongo to communicate with MongoDB to Morphia + new MongoDB Java Driver.
In the database there are flat documents like:
{
"_id" : ObjectId("56c2e5b9b9b6e9753d4f11de"),
"field1" : "foo",
"field2" : "bar",
"field3" : NumberLong("1455613369030"),
"field4" : NumberLong("1455613369030"),
"field5" : NumberLong(58),
"field6" : NumberLong(1)
}
And there are not flat entity classes to store those documents in Java app, annotated using jackson to be usable with 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…
}
}
Can I somehow port this exact structure to Morphia as is, without flattening the entity classes or expanding documents (so that foo
and bar
fields are in one embedded document and LongNumber
fields in another document)?
I have not found any examples of the @Embedded
annotation (the only one that looks relevant and gives some hope) to do such a thing. I want to end up with something like:
@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…
}
}