2013-07-28 26 views
0

可能的重複:Android How to make self-relationship in ORMLite?
我有同樣的問題與上述問題,但我沒有找到答案。
我的表是這樣的:ORMLite中的自我關係android

--------------------- 
    CATEGORY 
    --------------------- 
    id   (Integer) 
    name   (Text) 
    parentId  (Integer) 
    --------------------- 

我期待什麼:

@DatabaseTable (tableName = "Category") 
    public class Category{ 
     @DatabaseField (generatedId = true) 
     private int id; 

     @DatabaseField 
     private String name; 

     // how to map this one? 
     private Category parent; 
} 

請告訴我,我怎麼能映射父 - 子關係?我想用Category這樣的:

Category parent = new Category(); 
parent.name = "parent"; 

Category child = new Category(); 
child.setParent(parent); 

另一件事:我怎樣才能使用DAO類的所有兒童?我必須使用Sql查詢嗎?
在此先感謝。

回答

0

沒有測試它,我不知道是否會有任何錯誤,但你可以使用:

@DatabaseField(canBeNull = true, foreign = true) 
private Category parent; 

檢查,以防canBeNull = true一類是頂級類別,沒有父母。

@ForeignCollectionField 
private Collection<Category> childs; 

嘗試。

+0

我已編輯你的答案爲我的案件工作:刪除'foreignAutoRefresh'(它插入重複檢查外鍵時,它給了重複的記錄)和'eager'(因爲我不需要延遲加載),添加父ID的列名稱。現在它像一個魅力。你應該接受我的編輯。 – R4j