3

I'm implementing ActiveAndroid in my Android app, for that I'm referring this link. My problem definition is, I'm getting data list from server with help of retrofit library. After getting data list I'm inserting data into my "ActiveAndroid" DB. While debugging my app, I checked that, data for foreign key and other data members of model class is correct, but when I'm trying to get data back from "ActiveAndroid" I'm getting foreign key null. I googled a lot but yet unable to trace issue. For more information please check below details--

Currently I have following model classes-- 1] Event.java class

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

As shown in above "Event" model class, there is one foreign key i.e."Ground".

2] Ground.java class 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] This is how I'm storing Events in "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] This is how I'm getting Event list from "ActiveAndroid" DB

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

But after getting list from "ActiveAndroid", I'm getting "Ground" field as null. Please guide me where I'm getting wrong, also let me know if I can provide more information so that you can easily understand my problem. Thank you.!

1 Answers1

1

Please try below code it might help you.

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