2016-11-28 22 views
3

我在我的Android應用中實現ActiveAndroid,因爲我指的是​​3210。我的問題定義是,我正在通過改進庫的幫助從服務器獲取數據列表。獲取數據列表後,我將數據插入到我的「ActiveAndroid」數據庫中。在調試我的應用程序時,我檢查了模型類的外鍵和其他數據成員的數據是正確的,但是當我試圖從「ActiveAndroid」獲取數據時,我收到了外鍵null。我GOOGLE了很多,但無法追查問題。欲瞭解更多信息,請查看以下details--在ActiveAndroid中獲取外鍵成員「null」

目前,我有以下模型classes-- 1] Event.java類

@Table(name = "Events") 
public class Event extends Model 
{ 

    @Column(name = "eventID") 
    private int eventID; 
    @Column(name = "eventName") 
    private String eventName; 
    @Column(name = "Ground") 
    private Ground ground; 


    public Event() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public Event(int eventID, String eventName, 
      Ground ground) { 
     super(); 
     this.eventID = eventID; 
     this.eventName = eventName;  
     this.ground = ground;   
    } 

    public int getEventID() { 
     return eventID; 
    } 

    public void setEventID(int eventID) { 
     this.eventID = eventID; 
    } 

    public String getEventName() { 
     return eventName; 
    } 

    public void setEventName(String eventName) { 
     this.eventName = eventName; 
    } 

    public Ground getGround() { 
     return ground; 
    } 

    public void setGround(Ground ground) { 
     this.ground = ground; 
    }  
} 

如上文「事件」模型類所示,有一個外鍵即「地面」。

2] Ground.java類is--

Table(name = "Ground") 
public class Ground extends Model 
{ 
    @Column(name = "groundID") 
    private int groundID; 
    @Column(name = "groundName") 
    private String groundName; 

    public Ground() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public int getGroundID() { 
     return groundID; 
    } 

    public void setGroundID(int groundID) { 
     this.groundID = groundID; 
    } 

    public String getGroundName() { 
     return groundName; 
    } 

    public void setGroundName(String groundName) { 
     this.groundName = groundName; 
    } 

    public Ground(int groundID, String groundName) 
{ 
     super(); 
     this.groundID = groundID; 
     this.groundName = groundName; 
    } 
} 

3]這是我怎樣存儲在 「ActiveAndroid DB」 活動

ActiveAndroid.beginTransaction(); 
    try 
    { 
      for (int i = 0; i < list.size(); i++) 
      { 
       list.get(i).save(); 

       /* 
        Also tried--- 
        list.get(i).getGround.save(); 
        list.get(i).save(); 
       */ 
      } 
      ActiveAndroid.setTransactionSuccessful(); 
    } 
    finally 
    { 
      ActiveAndroid.endTransaction(); 
    } 

4]本是我如何從「ActiveAndroid」獲取事件列表數據庫

public static List<Event> getAllEvents() 
    { 
     return new Select() 
       .all() 
       .from(Event.class) 
       .execute(); 
    } 

但從「ActiveAndroid」獲取列表後,我將「Ground」字段設置爲null。請指導我出錯的地方,如果我可以提供更多信息,以便您可以輕鬆瞭解我的問題,請告訴我。謝謝。!

回答

0

請嘗試下面的代碼,它可能會幫助你。

@Table(name = "Events") 
public class Event extends Model { 
    @Column(name = "eventID") 
    private int eventID; 
    @Column(name = "eventName") 
    private String eventName; 
    @Column(name = "Ground", onUpdate = Column.ForeignKeyAction.CASCADE, onDelete = Column.ForeignKeyAction.CASCADE) 
    private Ground ground; 


    public Event() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public Event(int eventID, String eventName, 
       Ground ground) { 
     super(); 
     this.eventID = eventID; 
     this.eventName = eventName; 
     this.ground = ground; 
    } 

    public int getEventID() { 
     return eventID; 
    } 

    public void setEventID(int eventID) { 
     this.eventID = eventID; 
    } 

    public String getEventName() { 
     return eventName; 
    } 

    public void setEventName(String eventName) { 
     this.eventName = eventName; 
    } 

    public Ground getGround() { 
     return getMany(Ground.class, "Ground");//or/*new Select().from(Ground.class).execute();*/ 
    } 

    public void setGround(Ground ground) { 
     this.ground = ground; 
    } 
}