How to annotate inheritance classes using ORMLite?

I am trying to use inheritance with ORMLite, and I cannot work if it is supported or not, looking at the documentation and google search.

What I want to do is

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 } 

What I cannot work (provided it is supported) is how to annotate 3 classes for ORMLite.

Do I only need to annotate specific classes with @DatabaseTable(tableName = "Student") or do I need an abstract class?

I keep getting errors, for example:

04-24 10: 18: 30.857: E / AndroidRuntime (30495): caused by: java.lang.RuntimeException: java.sql.SQLException: unknown field 'name' from Android sqlite cursor, and not in: [year, school]

+6
source share
2 answers

The @DatabaseTable is only required in Student or Teacher tables and will not be used if it is in the base class Person .

What you need is the @DatabaseField annotation in the id and name fields in Person . For instance:

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

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

+7
source

Good for this, but now, how to implement the fourth class containing List<AbstractPerson> in this example?

I clarify 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 tries to access people like:

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

He will not be able to get personDAO because he does not exist (annotation) ... I get all my database functions with a good identifier and attitude, is it just a data access issue?

+1
source

Source: https://habr.com/ru/post/913996/


All Articles