2012-04-24 25 views
6

我正在嘗試使用ORMLite的繼承,如果它支持或不從查看文檔和搜索,我無法解決。如何正確註釋使用ORMLite的繼承類?

我想要做的是有

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 
} 

我不能工作了(假設它的支持)是如何註釋3類ORMLite。

我只需要用@DatabaseTable(tableName = "Student")註解具體類還是我還需要抽象類?

我不斷收到這樣的錯誤:

10月4日至24日:18:30.857:E/AndroidRuntime(30495):了java.lang.RuntimeException:致值java.sql.SQLException:未知的領域「名稱」從Android sqlite的光標,而不是在:今年,學校]

回答

7

@DatabaseTable註釋僅在StudentTeacher表必要,如果這是在Person基類將不會被使用。

你需要有一個@DatabaseField註釋在Personidname領域。例如:

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

ORMLite應該走的類層次結構和從所述基類的任何字段應該被包括在StudentTeacher表。如果您編輯您的問題以顯示@DatabaseField或其他註釋,我可以發表更多評論。

+0

謝謝,這也回答了我的問題,ORMLite是否適用於這種情況。 – srowley 2012-05-11 21:21:59

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和關係,這只是一個數據訪問問題?