How do I properly annotate inheritance classes using ORMLite?

本秂侑毒 提交于 2019-11-30 21:15:38

The @DatabaseTable annotation is only necessary on the Student or Teacher tables and would not be used if it was on the Person base class.

What you need to have is a @DatabaseField annotation on the id and name fields in Person. For example:

public abstract class Person{
    @DatabaseField(generatedId = true)
    public int id;
    @DatabaseField
    public String name;
}

ORMLite should walk the class hierarchy and any fields from the base class should be included in the Student and Teacher tables. If you edit your question to show the @DatabaseField or other annotations, I can comment more.

Ok for that but now, how to implements, in that example, a fourth class containing a List<AbstractPerson> ?

I precise my question :

public class ClassRoom {
    @ForeignCollectionField(foreignFieldName="asYouWant")
    public Collection<Person>   peoples;
}

peoples.add(new Student());
peoples.add(new Teacher());
peoples.add(new Student());

because when ormlite will try to access peoples like :

for (Person person : classRoom.peoples)
{
    if (person.getType() == Student)
        //do stuff
    else if (person.getType() == Student)
        //do other stuff
}

It won't be able to get personDAO because it doesn't exist (abstract)... I get all my database functionnal with good Id's and relation, it's just a data access question ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!