2017-07-31 69 views
0

如何在Ormlite中實現一對一的關係?假設我有實體國家資本ormlite與一對一的關係

@DatabaseTable(tableName = "countries") 
public class Country { 

    @DatabaseField(generatedId = true) 
    int id; 

    @DatabaseField 
    String name; 

    public Country() { 
    } 

} 

@DatabaseTable(tableName = "capitals") 
public class Capital { 

    @DatabaseField(generatedId = true) 
    int id; 

    @DatabaseField 
    String name; 

    public Capital() { 
    } 

} 

這是真的,我必須國家類這個領域?

@DatabaseField(foreign = true) 
Capital capital; 

...在資本類這個領域?

@DatabaseField 
Country country; 

回答

0

您只需要表中包含外鍵的類中的@DatabaseField。

如果國家包含capital_id,就應該添加

@DatabaseField(columnName = "capital_id", foreign = true) 
private Capital capital;