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

标签 android activeandroid

我正在我的 Android 应用程序中实现 ActiveAndroid,为此我指的是 this link .我的问题定义是,我在改造库的帮​​助下从服务器获取数据列表。获取数据列表后,我将数据插入到我的“ActiveAndroid”数据库中。在调试我的应用程序时,我检查了模型类的外键和其他数据成员的数据是否正确,但是当我尝试从“ActiveAndroid”取回数据时我得到的外键为空。我用谷歌搜索了很多但仍无法追踪问题。有关更多信息,请查看以下详细信息--

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

如上“Event”模型类所示,有一个外键,即“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”字段设为空。请指导我哪里出错了,如果我能提供更多信息,请告诉我这样你就可以很容易地理解我的问题。谢谢!

最佳答案

请尝试下面的代码,它可能对您有帮助。

 @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;
    }
}

关于android - 在 ActiveAndroid 中获取外键成员 "null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837867/

相关文章:

android - 微调器中的默认文本填充是什么

android - 房间与活跃的安卓

android - 带有 robolectric 的 sqlcipher 的 UnsatisfiedLinkError

java - 在 ActiveAndroid 中是否可以在不清除行中其余数据的情况下更新表行中的特定字段

android - 我无法保存关联模型 (ActiveAndroid)

javascript - Android 4.4.2 html 文件上传是否正常?需要一个解决方案而不是解决方法

java - 如何显示Android的sensorevent.timestamp的准确时间?

android - 如何在 Android SDK 模拟器中运行基于 GPS 的应用程序 (WikiTude)?

android - Raspberry Pi 的多个摄像头

java - 枚举字段上的 ActiveAndroid NoClassDefFoundError 异常