0

Father

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

Child

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

Another Class

    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);


}

Its returning

sponsorServer1 = SponsorServer(timestamp=1276605030, sponsorLists=null)

How can i save the child in father object?

How Relationships works on activeandroid?

I didnt understand in

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

tks

2 Answers2

1

Based on ActiveAndroid github and my project this is how relationships should work:

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

}
Alex
  • 81
  • 7
0

Related to the link that you attached, you should complete the solution above with these 3 steps:

1.) Put a column (and it's setter) in child (SponsorList) which will refer to the parent (SponsorServer).

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

2.) When you save the SponsorLists, tell the SponsorList item who is it's parent and after that save it.

sponsor.sponsorServer = sponsorServer;

3.) Make the sponsorLists in SponsorServer private and write a getter method in order to reach the relative SponsorLists in the database.

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

After these modifications you can access the SponsorServer's SponsorLists with the getSponsorLists method.

The full code will look like this:

Parent:

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

Child:

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

Code part of saving:

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);
    }
}
Gex
  • 2,092
  • 19
  • 26