Entity inheritance

212 views
Skip to first unread message

samsy

unread,
Aug 13, 2012, 6:11:28 AM8/13/12
to mor...@googlegroups.com
Hi, this newbie question is about subclassing @Entity class - is it possible and how should it be done?

So, i have two classes:

@Entity
public abstract class ModelObject implements Serializable {
@Id private String id = new ObjectId().toString();
private Date created = new Date();
@Version private long modified;
}

@Entity
public class Test extends ModelObject {
private int test = 10;
// accessors
}


now if i have test code:

BasicDAO<Test, String> dao = new BasicDAO<Test, String>(Test.class, ds);
Test t = new Test();
dao.save(s);


then nothing is stored to data base. I guess this is because annotations are not inherited so Morphia not get @Id declaration of Test. But is it possible to define superclass for entities with Morphia and how?

Scott Hernandez

unread,
Aug 13, 2012, 4:41:51 PM8/13/12
to mor...@googlegroups.com
As far as morphia is concerned this should result in a class which
looks like this:

class Test {
@Id String id;
Date created;
int test; }

What is getting saved in the database? Can you do a db.Test.findOne()
from the shell?

samsy

unread,
Aug 14, 2012, 4:20:21 AM8/14/12
to mor...@googlegroups.com
Okay, that's correct. When i tested with production entities it really does work perfectly. But still, with my test code, i am not able to get object stored to db - db.Test.findOne() just gets null. It must be that there is some error in my test code, i will attach it here as it now is


@Entity
public abstract class ModelObject implements Serializable {
   
    @Id private String id = new ObjectId().toString();
    private Date created = new Date();
    @Version private long modified;
   
    // accessors + toString...

}

@Entity("testentity")
public class TestEntity extends ModelObject {
   
    private int test = 12345;

    // accessors + toString
}

public class Test {

    public static void main(String[] args) throws Exception {
       
        Mongo connection = new Mongo();
        Morphia morphia = new Morphia();
        morphia.map(TestEntity.class);
        Datastore ds = morphia.createDatastore(connection, "test_db");
        BasicDAO<TestEntity, String> dao = new BasicDAO<TestEntity, String>(TestEntity.class, ds);
       
        TestEntity entity = new TestEntity();
        dao.save(entity);
        System.out.println("should be saved = "+entity);
    }
}

after running this, and in mongo: use test_db; db.testentity.findOne(); gets null. But if I change TestEntity to not inherit from ModelObject and give it own @Id private String id = new ObjectId().toString(); then it will save to db_test.testentity. So i am little confused, inheritance works with real entities but not with above test code.

Sampo Yrjänäinen

unread,
Aug 14, 2012, 5:59:56 AM8/14/12
to mor...@googlegroups.com
Few tests more and it seems if i run this code from java application nothing gets persisted. But if i run it from EJB bean, it will work. So there is something strange about my environment i guess. But still, if I remove inheritance from entity then entities are persisted when ran in java application too. Dosen't really matter to me, because it works with EJB, but any guesses what could cause this?

Scott Hernandez

unread,
Aug 14, 2012, 8:18:55 AM8/14/12
to mor...@googlegroups.com
Nothing comes to mind but if you can reproduce it in an isolated set
of classes I can try running it.

What appserver are you using and what ejb version/implementation?
Reply all
Reply to author
Forward
0 new messages