Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay Morphia, Cannot grab List Content of (Generic) Embedded Object
    text
    copied!<p>I am working with Play Framework 1.2.5 and the morphia-1.2.9a module. I was originally using @Reference annotations on my models but due to the fact that (lazy=true) is not working on @Reference's, I needed to switch using ObjectIds. In an attempt to reduce rework and redundancies, I created the following MorphiaList:</p> <pre><code>package models.morphia; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import models.Team; import org.bson.types.ObjectId; import com.google.code.morphia.annotations.Embedded; import com.google.code.morphia.annotations.Transient; import play.modules.morphia.Model; @Embedded public class MorphiaList&lt;T extends MorphiaModel&gt; implements Iterable&lt;T&gt;{ @Embedded("ids") private List&lt;ObjectId&gt; modelIds; @Transient List&lt;T&gt; models; public void sort() { this.getAll(); Collections.sort(models); this.modelIds.clear(); for(T t:this.models){ this.modelIds.add((ObjectId)t.getId()); } } public MorphiaList() { this.models = new ArrayList&lt;T&gt;(); this.modelIds = new ArrayList&lt;ObjectId&gt;(); } public int size() { System.out.println(modelIds.size()); return modelIds.size(); } public boolean add(T model) { return (this.models.add(model) &amp;&amp; this.modelIds.add((ObjectId) model.getId())); } public boolean remove(T model) { return (this.models.remove(model)&amp;&amp;this.modelIds.remove((ObjectId) model.getId())); } public boolean addAll(Collection&lt;? extends T&gt; models) { for(T model: models) { this.models.add(model); this.modelIds.add((ObjectId) model.getId()); } return false; } public List&lt;T&gt; getAll() { for(ObjectId oi: this.modelIds) { boolean found=false; for(T model: models) { if(oi==model.getId()) { found=true; break; } } if(!found) models.add(T.&lt;T&gt;findById(oi)); } return models; } public T get(int index) { ObjectId id = modelIds.get(index); for(T model: models) { if(id == model.getId()) return model; } return T.&lt;T&gt;findById(id); } @Override public Iterator&lt;T&gt; iterator() { return this.getModelIterator(); } public Iterator&lt;T&gt; getModelIterator() { for(ObjectId oi: modelIds) { boolean found=false; for(T model: models) { if(oi==model.getId()) { found=true; break; } } if(!found) models.add(T.&lt;T&gt;findById(oi)); } return models.iterator(); } public boolean isEmpty() { return this.modelIds.isEmpty(); } public boolean contains(T t) { return this.modelIds.contains(t.getId()); } } </code></pre> <p>MorphiaModel is simple, I just needed it to implement Comparable so I could sort my lists:</p> <pre><code>package models.morphia; import play.modules.morphia.Model; public abstract class MorphiaModel&lt;T extends MorphiaModel&lt;T&gt;&gt; extends Model implements Comparable&lt;T&gt;{ } </code></pre> <p>Then I use it like this in my classes:</p> <pre><code>@Entity public class Organization extends Purchasable { @Embedded public MorphiaList&lt;Season&gt; seasons = new MorphiaList&lt;Season&gt;(); </code></pre> <p>Once I add a season, mongo shows the expected:</p> <pre><code>{ "_id" : { "$oid" : "50097c147aa33c43fa8136ee"} , ...other members... "seasons" : { "ids" : [ { "$oid" : "50097c147aa33c43fa8136ed"}]} , ...other members... } </code></pre> <p>When I go to grab the seasons object though, modelIds is empty! I'm not sure why it can't grab seasons from the BSON, it's obviously there. Any help is greatly appreciated!</p>
 

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