1

I am currently playing with Room in order to compare it with Realm and I already have so many question about the best way to do things.

In my sample app, I have a really simple model in which one a Person can have Cats and Dogs.

Here the java classes.

Cat and Dog classes inherit from an Animal class :

public abstract class RoomAnimal
{

  @PrimaryKey
  public int id;

  public int age;

  public String name;

}

The Cat class :

@Entity(tableName = "cat")
public final class RoomCat
    extends RoomAnimal
{

}

The Dog class :

@Entity(tableName = "dog")
public final class RoomDog
    extends RoomAnimal
{

  public enum RoomColor
  {
    Black, White
  }

  public static final class RoomColorConverter
  {

    @TypeConverter
    public RoomColor fromString(String color)
    {
      return color != null ? RoomColor.valueOf(color) : null;
    }

    @TypeConverter
    public String fromRealmColor(RoomColor color)
    {
      return color.toString();
    }

  }

  @TypeConverters(RoomColorConverter.class)
  public RoomColor color;

}

The Person class :

@Entity(tableName = "person")
public final class RoomPerson
{

  @PrimaryKey
  public int id;

  public String name;

}

I also have a POJO in order to modelize the fact that a user can have cats and dogs :

public final class RoomPersonWithAnimals
{

  @Embedded
  public RoomPerson person;

  @Relation(parentColumn = "id", entityColumn = "id", entity = RoomDog.class)
  public List<RoomDog> dogs;

  @Relation(parentColumn = "id", entityColumn = "id", entity = RoomCat.class)
  public List<RoomCat> cats;

}

The question is : how to save a list of RoomPersonWithAnimals objects ?

I cannot use the Insert annotation in a Dao class because the RoomPersonWithAnimals is not an Entity.

For each objects of my list of RoomPersonWithAnimals should I run 3 requests ?

  • one in order to insert the person attribute ;
  • one in order to insert the list of cats ;
  • one in order to insert the list of dogs ;

Thank you in advance for your help !

rolandl
  • 1,769
  • 1
  • 25
  • 48

1 Answers1

2

how to save a list of RoomPersonWithAnimals objects ?

You don't, at least in the 1.0.0-alpha6 version of Room, as those are not entities.

For each objects of my list of RoomPersonWithAnimals should I play 3 requests ?

Yes. You can use runInTransaction() to perform those three operations inside a single SQLite transaction.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thk you for your answer. Can you confirm me that my POJO is the right way to produce a 1 to many relations between entities ? – rolandl Jul 26 '17 at 19:46
  • @rolandl: I would describe it as "a way of atomically retrieving related entities". You do not have to use `@Relation` for this. You can use `runInTransaction()` and use other entity-based DAO methods you want for pulling in the contents for your view model (or whatever). So, `@Relation` is an option but not a requirement. – CommonsWare Jul 26 '17 at 19:52
  • @CommonWare : Thank your for your answer. But how can I produce a 1 to many relation using only a foreign key ? For example if I want to modelize the fact that a dog can be held by several persons. I cannot a the person foreign key into my animal class. Can an entity hold a list of foreign key ? I do not see this on the documentation. – rolandl Jul 26 '17 at 20:07
  • @rolandl: "I cannot a the person foreign key into my animal class" -- why not? – CommonsWare Jul 26 '17 at 20:10
  • @CommonWare : if I do that, how to manage the fact that a dog can be held by one, two or more persons ? – rolandl Jul 26 '17 at 21:16
  • @rolandl: Then you have an M:N, not a 1:N, relationship, since presumably a person can have more than one cat or dog. That will require a join table (modeled as a Room entity). – CommonsWare Jul 26 '17 at 21:17
  • @CommonWare : yes you're right ! Can you provide an example of a join table modeled as a Room entity ? – rolandl Jul 26 '17 at 21:58