pbattisson
pbattisson

Reputation: 1240

@Embedded Reference on Entity Giving java.lang.stackoverflow

We have the following error when attempting to use an @Embedded object in Morphia on Play.

A java.lang.StackOverflowError has been caught, null Hide trace
java.lang.StackOverflowError
at java.util.LinkedHashMap.init(LinkedHashMap.java:223)
at java.util.HashMap.(HashMap.java:210)
at java.util.LinkedHashMap.(LinkedHashMap.java:181)
at org.bson.BasicBSONObject.(BasicBSONObject.java:39)
at com.mongodb.BasicDBObject.(BasicDBObject.java:42)
at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:435)
at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:430)
at com.google.code.morphia.mapping.EmbeddedMapper.writeCollection(EmbeddedMapper.java:68)
at com.google.code.morphia.mapping.EmbeddedMapper.toDBObject(EmbeddedMapper.java:30)

This is caused by trying to save an instance of the following class:

@Entity
public class Profile extends Model
{
@Embedded
public class ObjectPermission
{
    public String type;
    public ArrayList<String> viewable;
    public ArrayList<String> editable;
}

public String _sfid;
public String _type;
@Embedded
public ArrayList<ObjectPermission> object;
}

We are attempting to save it in the following way:

@Test
public void TestFullProfileSave()
{
    Profile p = new Profile();
    p._sfid = "0123456789101213145";
    p._type = "entitlements";
    ObjectPermission objPerm = p.new ObjectPermission();
    objPerm.type = "Account";
    objPerm.viewable = new ArrayList<String>();
    objPerm.viewable.add("field1");
    objPerm.viewable.add("field2");
    objPerm.editable = new ArrayList<String>();
    objPerm.editable.add("field3");
    objPerm.editable.add("field4");
    p.object = new ArrayList<ObjectPermission>();
    p.object.add(objPerm);
    p.save();
}

I have a feeling it is the ArrayLists but don't know why hence my confusion. Thanks in advance.

Paul

Upvotes: 0

Views: 1098

Answers (2)

Scott Hernandez
Scott Hernandez

Reputation: 7590

In newer versions of morphia you will get a better error message about needing to declare the inner class as 'static'. That is the problem here.

public static class ObjectPermission { ...

Upvotes: 2

BitSchupser
BitSchupser

Reputation: 441

As I remember you'd have to annotate the class with @Embeddable and only the reference as @Embedded. But usually you'd do this with references to single objects. I don't think you can embedd whole collections. You should rather user @OneToMany to map the Objectpermission. That would cause a separate mapping table.

Upvotes: -1

Related Questions