我正在尝试将继承与ORMLite一起使用,如果通过查看文档和Google搜索来支持或不支持,我就无法解决。
我想做的是
public abstract class Person{
public int id;
public String name;
}
public class Student extends Person{
public String school;
public String year;
// other student stuff
}
public class Teacher extends Person{
public String title;
// other teacher stuff
}
我无法解决的问题(假设它得到支持)是如何为ORMLite注释3个类。
我只需要使用@DatabaseTable(tableName = "Student")
注释具体类,还是需要抽象类?
我一直收到如下错误:
04-24 10:18:30.857:E / AndroidRuntime(30495):引起:java.lang.RuntimeException:java.sql.SQLException:来自Android sqlite游标的未知字段'name',而不是:[year ,学校]
答案 0 :(得分:7)
@DatabaseTable
注释仅在Student
或Teacher
表上是必需的,如果它位于Person
基类上,则不会被使用。
您需要拥有的是@DatabaseField
中id
和name
字段Person
注释。例如:
public abstract class Person{
@DatabaseField(generatedId = true)
public int id;
@DatabaseField
public String name;
}
ORMLite应该遍历类层次结构,基类中的任何字段都应包含在Student
和Teacher
表中。如果您编辑问题以显示@DatabaseField
或其他注释,我可以发表更多评论。
答案 1 :(得分:1)
好的,但是现在,在这个例子中,如何实现包含List<AbstractPerson>
的第四个类?
我确切地提出了我的问题:
public class ClassRoom {
@ForeignCollectionField(foreignFieldName="asYouWant")
public Collection<Person> peoples;
}
peoples.add(new Student());
peoples.add(new Teacher());
peoples.add(new Student());
因为ormlite会尝试访问以下人员:
for (Person person : classRoom.peoples)
{
if (person.getType() == Student)
//do stuff
else if (person.getType() == Student)
//do other stuff
}
它将无法获得personDAO,因为它不存在(抽象)...... 我得到的所有数据库功能都具有良好的Id和关系,它只是一个数据访问问题?