Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:28:51+00:00 2026-06-08T10:28:51+00:00

I am working with Play Framework 1.2.5 and the morphia-1.2.9a module. I was originally

  • 0

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:

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 is simple, I just needed it to implement Comparable so I could sort my lists:

package models.morphia;
import play.modules.morphia.Model;
public abstract class MorphiaModel<T extends MorphiaModel<T>> extends Model implements Comparable<T>{
}

Then I use it like this in my classes:

@Entity
public class Organization extends Purchasable { 
@Embedded public MorphiaList<Season> seasons = new MorphiaList<Season>();

Once I add a season, mongo shows the expected:

{ 
 "_id" : { "$oid" : "50097c147aa33c43fa8136ee"} ,
 ...other members...
 "seasons" : { "ids" : [ { "$oid" : "50097c147aa33c43fa8136ed"}]} ,
 ...other members...
}

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:28:55+00:00Added an answer on June 8, 2026 at 10:28 am

    I could not get this to work for multiple reasons:

    1. I could not get the data once persisted to the mongoDB to load back into the object, the object would always just be null.
    2. After issue #1, I added a private list that would populate my MorphiaList on lookup to get around it. But after getting the ids into the MorphiaList, I could not figure out how to do a lookup on the specific collections. Especially when you consider I use inheritance so I have some heterogeneous collections.

    My Solution: For every List of references I rewrote this:

    @Reference public List<Season> seasons;
    

    To this:

    @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;
    }
    

    I also had to add a @PrePersist method to check for list access:

    @PrePersist void beforeSave()
    {
        if(pSeasons!=null)
        {
            _seasons.clear();
            for(Season s: pSeasons)
            {
                _seasons.add((ObjectId)s.getId());
            }
        }
    }
    

    This beforeSave() will have to expand for every extra List of refs you have in your code. Moral of the story:

    If you’re using Play’s morphia module, don’t use the @Reference notation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Secure module from play framework and its mostly working great.
i'me working on a web server using play framework 2.0, where the login is
I have a Play Framework application that sends emails using SMTP server. Now I
I'm using Heroku to deploy my Play! framework app. I have a form that
I'm working on an iOS application that needs to play some sounds using the
An app I'm working on using the Play! Framework has an object called gift
I have a working Play Framework project. I'm using Play Framework version number 1.2.5
I am working on Play Framework 2.0 and it uses Jerkson to parse JSON
I'm trying to play a random sound onTouch event (currently working), but the sound
I've been working on a Play framework social networking application, and I've come across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.