pardom-zz / ActiveAndroid

Active record style SQLite persistence for Android
http://www.activeandroid.com
4.7k stars 1.03k forks source link

How to store nested object ActiveAndroid? #534

Open JuanBGo opened 7 years ago

JuanBGo commented 7 years ago

I'm working with ActiveAndroid library, I have the following problem: I hace two class for my database. Person and PersonPhoto. Every person has only one photo, and photo is only for one person.

These is my code.

@Table(name = "Person")
public class Person extends Model{
    @Column(name = "name")
    public String name;
    @Column(name = "photo")
    public PersonPhoto photo ;

   public Person() {}
}

this is my second class.


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

    @Column(name = "name")
    public String name;
    @Column(name = "photo64Code")
    public String photo64Code;    

    public PersonPhoto() {}
}

In my Activity I have two buttons one for make an random insert and the other one for make and a general retreaving. here the code:

 @Override
            public void onClick(View view) {
            Person p = new Person();
            PersonPhoto photo = new PersonPhoto();

            photo.setName("photo");
            photo.setPhoto64Code("011110");

            p.setNickName("user");
            p.setPassword("123");
            p.setName("user");
            p.setfLastName("usera");
            p.setsLastName("userb");
            p.setLoginStatus(1);
            p.setPhoto(photo);

            p.save();
            p.getPhoto().save();

           }

my other button calls these method:

private List<Person> getAll() {
    return new Select()
            .from(Person.class)
            .orderBy("id ASC")
            .execute();
}

I ran it with my debugger and I saw my list details, all is okay individually But in my Person object I don't have my photo reference. its marked like a null.

I don't know how to insert a nested object with ActiveAndroid.

Thanks for read.