Playframework 播放Morphia,无法获取(通用)嵌入对象的列表内容

Playframework 播放Morphia,无法获取(通用)嵌入对象的列表内容,playframework,morphia,playframework-1.x,Playframework,Morphia,Playframework 1.x,我正在使用Play Framework 1.2.5和morphia-1.2.9a模块。我最初在模型上使用@Reference注释,但由于(lazy=true)不在@Reference上工作,我需要切换使用objectid。为了减少返工和冗余,我创建了以下形态学家: package models.morphia; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; impor

我正在使用Play Framework 1.2.5和morphia-1.2.9a模块。我最初在模型上使用@Reference注释,但由于(lazy=true)不在@Reference上工作,我需要切换使用objectid。为了减少返工和冗余,我创建了以下形态学家:

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());
    }
}

但当我去抓取seasons对象时,modelIds是空的!我不知道为什么它不能从BSON那里抓住季节,它显然在那里。非常感谢您的帮助

由于多种原因,我无法实现此功能:

  • 一旦将数据持久化到mongoDB并加载回对象中,我就无法获取数据,该对象总是空的
  • 在问题#1之后,我添加了一个私有列表,可以在查找时填充我的语素学家来绕过它。但是,在将ID放入morphiliast之后,我不知道如何查找特定集合。特别是当您考虑使用继承时,我有一些异构的集合。
  • 我的解决方案:对于每个参考文献列表,我都重写了以下内容:

    @Reference public List<Season> seasons;
    
    beforeSave()必须为代码中的每个额外引用列表展开。故事的寓意:

    如果您正在使用Play的morphia模块,请不要使用@Reference符号

    @Entity
    public class Organization extends Purchasable { 
    @Embedded public MorphiaList<Season> seasons = new MorphiaList<Season>();
    
    { 
     "_id" : { "$oid" : "50097c147aa33c43fa8136ee"} ,
     ...other members...
     "seasons" : { "ids" : [ { "$oid" : "50097c147aa33c43fa8136ed"}]} ,
     ...other members...
    }
    
    @Reference public List<Season> seasons;
    
    @Transient public List<Season> seasons;
    private List<ObjectId> _seasons = new ArrayList();
    @Transient private List<Season> pSeasons;
    public List<Season> getSeasons()
    {
        if(pSeasons==null)
        {
            if(_seasons.size()!=0)
                pSeasons = Season.find().<Season>field("_id").in(_seasons).asList();
            else
                pSeasons = new ArrayList();
        }
        return pSeasons;
    }
    
    @PrePersist void beforeSave()
    {
        if(pSeasons!=null)
        {
            _seasons.clear();
            for(Season s: pSeasons)
            {
                _seasons.add((ObjectId)s.getId());
            }
        }
    }