在ActiveAndroid中获取外键成员"null"

3
我正在将ActiveAndroid应用于我的Android应用程序中,为此我参考了this link。我的问题定义是,我使用retrofit库从服务器获取数据列表。在获取数据列表后,我将数据插入到我的“ActiveAndroid”数据库中。在调试我的应用程序时,我检查了模型类的外键和其他数据成员的数据是正确的,但是当我尝试从“ActiveAndroid”中获取数据时,我得到的外键是null。我搜索了很多,但仍然无法找到问题所在。有关更多信息,请查看下面的详细信息 -

目前我有以下模型类 - 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;
    }    
}

如上所示的“事件”模型类中,有一个外键“Ground”。
2] Ground.java类是 -
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。请指导我哪里出错了,还请让我知道是否可以提供更多信息,以便您可以轻松理解我的问题。谢谢!
1个回答

1
请尝试以下代码,它可能会对您有所帮助。
 @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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接