Activeandroid关系子null

时间:2015-01-14 20:41:24

标签: android database activeandroid

父亲

@Data  
        @Table(name = "SponsorServer")
        public class SponsorServer extends Model {

        @SerializedName("timestamp")
        @Column(name = "Timestamp")
        private String timestamp;

            @SerializedName("sponsorsList")
            @Column(name = "SponsorLists", onUpdate = Column.ForeignKeyAction.CASCADE, onDelete = Column.ForeignKeyAction.CASCADE)
            public List<SponsorList> sponsorLists;
        }

    @Data
    @Table(name = "SponsorList")
    public class SponsorList extends Model {

        @SerializedName("id")
        @Column(name = "SponsorListID", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
        private int sponsorListID;

        @SerializedName("order")
        @Column(name = "SponsorListOrder")
        private int order;


        @SerializedName("name")
        @Column(name = "Name")
        private String name;
}

另一个班级

    ActiveAndroid.beginTransaction();

try {

   for (SponsorList sponsorList : sponsorServer.getSponsorLists()) {


     for (Sponsor sponsor : sponsorList.getSponsors()) {
        sponsor.save();
     }

   sponsorList.save();

}

sponsorServer.save();

ActiveAndroid.setTransactionSuccessful();

} finally {

ActiveAndroid.endTransaction();

SponsorServer sponsorServer1 = new Select().all().from(SponsorServer.class).executeSingle();

Log.d("", "-------- sponsorServer1 = " + sponsorServer1);


}

返回

sponsorServer1 = SponsorServer(时间戳= 1276605030,sponsorLists = null)

如何将孩子保存在父对象中?

关系如何在activeandroid上运行?

我不明白

https://github.com/pardom/ActiveAndroid/wiki/Creating-your-database-model

TKS

2 个答案:

答案 0 :(得分:1)

基于ActiveAndroid github和我的项目,关系应该如何运作:

@Table(name = "Sesje")
public class ChildEntity extends Model {

    @Column(name = "FatherEntity", onDelete = ForeignKeyAction.CASCADE)
    public FatherEntity fatherEntity;

}


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

    public List<ChildEntity> children(){
        return getMany(ChildEntity.class, "FatherEntity");
    }

}

答案 1 :(得分:0)

与您附加的链接相关,您应该通过以下3个步骤完成上述解决方案:

1。)在子(SponsorList)中放置一个列(和它的setter),它将引用父(SponsorServer)。

@Column(name = "SponsorServer")
public SponsorServer sponsorServer;

2。)当你保存赞助商名单时,告诉赞助商名单项目是谁的父母,然后保存它。

sponsor.sponsorServer = sponsorServer;

3。)将SponsorServer中的赞助商列表设为私有,并编写一个getter方法,以便访问数据库中的相关SponsorLists。

public List<SponsorList> getSponsorLists() {
    if(sponsorLists == null){
        sponsorLists = getMany(SponsorList.class, "SponsorServer");
    }
    return sponsorLists;
}

经过这些修改后,您可以使用getSponsorLists方法访问SponsorServer的SponsorLists。

完整代码如下所示:

家长:

@Data  
@Table(name = "SponsorServer")
public class SponsorServer extends Model {

    @SerializedName("timestamp")
    @Column(name = "Timestamp")
    private String timestamp;

    @SerializedName("sponsorsList")
    private List<SponsorList> sponsorLists;

    public List<SponsorList> getSponsorLists() {
        if(sponsorLists == null){
            sponsorLists = getMany(SponsorList.class, "SponsorServer");
        }
        return sponsorLists;
    }
}

子:

@Data
@Table(name = "SponsorList")
public class SponsorList extends Model {

    @SerializedName("id")
    @Column(name = "SponsorListID", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    private int sponsorListID;

    @SerializedName("order")
    @Column(name = "SponsorListOrder")
    private int order;


    @SerializedName("name")
    @Column(name = "Name")
    private String name;

    @Column(name = "SponsorServer")
    public SponsorServer sponsorServer;
}

保存的代码部分:

ActiveAndroid.beginTransaction();

try {

    for (SponsorList sponsorList : sponsorServer.getSponsorLists()) {

        for (Sponsor sponsor : sponsorList.getSponsors()) {
            sponsor.sponsorServer = sponsorServer;
            sponsor.save();
        }

        sponsorList.save();
    }

    sponsorServer.save();

    ActiveAndroid.setTransactionSuccessful();

} finally {

    ActiveAndroid.endTransaction();

    SponsorServer sponsorServer1 = new Select().all().from(SponsorServer.class).executeSingle();

    Log.d("", "-------- sponsorServer1 Lists= ");
    for(SponsorList item : sponsorServer1.getSponsorLists()){
        Log.d("", "" + item);
    }
}