我正在使用 Play Framework 1.2.5 和 morphia-1.2.9a 模块。我最初在我的模型上使用@Reference 注释,但由于 (lazy=true) 不适用于@Reference,我需要使用 ObjectIds 进行切换。为了减少返工和冗余,我创建了以下 MorphiaList:
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<T extends MorphiaModel> implements Iterable<T>{
@Embedded("ids") private List<ObjectId> modelIds;
@Transient List<T> 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<T>();
this.modelIds = new ArrayList<ObjectId>();
}
public int size()
{
System.out.println(modelIds.size());
return modelIds.size();
}
public boolean add(T model)
{
return (this.models.add(model) && this.modelIds.add((ObjectId) model.getId()));
}
public boolean remove(T model)
{
return (this.models.remove(model)&&this.modelIds.remove((ObjectId) model.getId()));
}
public boolean addAll(Collection<? extends T> models)
{
for(T model: models)
{
this.models.add(model);
this.modelIds.add((ObjectId) model.getId());
}
return false;
}
public List<T> 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.<T>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.<T>findById(id);
}
@Override
public Iterator<T> iterator() {
return this.getModelIterator();
}
public Iterator<T> getModelIterator() {
for(ObjectId oi: modelIds)
{
boolean found=false;
for(T model: models)
{
if(oi==model.getId())
{
found=true;
break;
}
}
if(!found)
models.add(T.<T>findById(oi));
}
return models.iterator();
}
public boolean isEmpty() {
return this.modelIds.isEmpty();
}
public boolean contains(T t) {
return this.modelIds.contains(t.getId());
}
}
MorphiaModel 很简单,我只需要它来实现 Comparable 以便我可以对列表进行排序:
package models.morphia;
import play.modules.morphia.Model;
public abstract class MorphiaModel<T extends MorphiaModel<T>> extends Model implements Comparable<T>{
}
然后我在课堂上这样使用它:
@Entity
public class Organization extends Purchasable {
@Embedded public MorphiaList<Season> seasons = new MorphiaList<Season>();
一旦我添加了一个季节,mongo 就会显示预期的:
{
"_id" : { "$oid" : "50097c147aa33c43fa8136ee"} ,
...other members...
"seasons" : { "ids" : [ { "$oid" : "50097c147aa33c43fa8136ed"}]} ,
...other members...
}
但是,当我去抓取 seasons 对象时,modelIds 是空的!我不知道为什么它不能从 BSON 那里抢到季节,它显然就在那里。任何帮助是极大的赞赏!