Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use a way to handle it. Not sure it's perfect but it works for myself.</p> <p>Let's say you have these classes</p> <pre><code>public class MyObject { @Id private String id; private MyEmbeddedObject embeddedObject; public String getId() { return this.id; } public void setId(String id) { this.id = id; } public MyEmbeddedObject getEmbeddedObject() { return this.embeddedObject; } public void setEmbeddedObject(MyEmbeddedObject embeddedObject) { this.embeddedObject = embeddedObject; } } public class MyEmbeddedObject { @Id private String id; public String getId() { return this.id; } public void setId(String id) { this.id = id; } } </code></pre> <p>You want to store MyEmbeddedObject in collection "embed" and Object in collection "object" with a manual reference to "embed" collection ids. For example :</p> <pre><code>object : { id:1, embeddedObjectId: 99 } embed : { id : 99 } </code></pre> <p>You can implement a custom serializer and deserializer for the MyEmbeddedObject</p> <pre><code>public class MyEmbeddedObjectSerializer extends JsonSerializer&lt;MyEmbeddedObject&gt; { @Override public void serialize(MyEmbeddedObject value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeText(value.getId()); } } public class MyEmbeddedObjectDeserializer extends JsonDeserializer&lt;MyEmbeddedObject&gt; { @Override public MyEmbeddedObject deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { MyEmbeddedObject obj = new MyEmbeddedObject(); obj.setId(jsonParser.getString()); return new MyEmbeddedObject(); } } </code></pre> <p>If your register your serializer in Jongo, the problem is that it will be used for MyEmbeddedObject as a member of MyObject but also for MyEmbeddedObject when it is stored by itself in the "embed" collection.</p> <p>To apply the custom serizalizers/deserializers only when MyEmbeddedObject is embed in MyOBject, I use Mixin annotations.</p> <pre><code>public interface MyObjectMixIn { @JsonSerialize(using=MyEmbeddedObjectSerializer .class) public MyEmbeddedObject getEmbeddedObject(); @JsonDeserialize(using=MyEmbeddedObjectDeserializer.class) public void setEmbeddedObject(MyEmbeddedObject embeddedObject); } </code></pre> <p>Then define a module</p> <pre><code>public class MyModule extends SimpleModule { @Override public void setupModule(SetupContext context) { context.setMixInAnnotations(MyObject.class, MyObjectMixIn.class); } } </code></pre> <p>Finaly register the module in Jongo</p> <pre><code>Jongo jongo = new Jongo(db, JacksonMapper.Builder().registerModule(new MyModule ()).build()); </code></pre> <p>Now to use it for storage in Mongo</p> <ol> <li>Store the embeddedObject by itself in collection "embed" to get an id</li> <li>Be sure the embeddedObject member of object contains the id</li> <li>Store the object in collection "object". Only the id of the embedded object will be stored</li> </ol> <p>And to read datastore</p> <ol> <li>Read the object from collection "object". The embeddedobject will be initialize only with its ID</li> <li>If you need it, read the embeddedObject in collection "embed".</li> </ol> <p>EDIT : to avoid lazy loading and load the complete object at deserialization, <a href="http://wiki.fasterxml.com/HandlerInstantiator" rel="nofollow">HandlerInstantiator</a> object from Jackson is useful to customize serializer and deserializer construction mechanism. </p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload