Joining tables in a ROOM database
D

1

6

This is my first ROOM implementation. I have a User class which has a list of Pets as one of its member variables.

public class User {
    String id;
    String name;
    List<Pet> pets;
}


Public class Pets {
    String id;
    String type;
}

How to create its Entity and DAO classes for Room Database from the following JSON?

{  
  "users":[  
    {  
      "id":"as123",
      "name":"John",
      "pets":[  
        {  
          "id":"p123",
          "type":"dog"
        }
      ]
    },
    {  
      "id":"as343",
      "name":"Mark",
      "pets":[  
        {  
          "id":"p324",
          "type":"dog"
        },
        {  
          "id":"p254",
          "type":"cat"
        }
      ]
    }
  ]
}

I have created the following but not sure how to create a correct column or Join these tables.

Entity Classes

@Entity(tableName = "user")
public class User {

    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "id")
    String id;

    @Nullable
    @ColumnInfo(name = "name")
    String name;

    List<Pets> pets; // how to set type for Pets
}

@Entity(tableName = "pets",
    foreignKeys = @ForeignKey(entity = User.class,
    parentColumns = "id",
    childColumns = "userId",
    onDelete = CASCADE))
Public class Pets {

    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "id")
    String id;

    @Nullable
    @ColumnInfo(name = "type")
    String type;

    @NonNull
    @ColumnInfo(name = "userId")
    String userId; // for accessing pets of a user
} 

DAO classes

@Dao
public interface PetsDAO {

    @Insert
    void insert(Pets pets);

    @Update
    void update(Pets... pets);

    @Delete
    void delete(Pets... pets);

    @Query("SELECT * FROM pets WHERE id=:userId")
    List<Pets> getPetsForUser(final String userId);
}

@Dao
public interface UserDAO {

    @Insert
    void insert(User pets);

    @Update
    void update(User... pets);

    @Delete
    void delete(User... pets);

    @Query("SELECT * FROM user)
    List<User> getAllUsers(); // should return list of users with all their pets
}

How can I get a List of users with all of their respective pets?

Dauphin answered 5/7, 2018 at 8:34 Comment(0)
D
3

This is a many-to-many relation so you have to create a join table: UserPetsJoin table with the petId and userId; Here you can find a good topic explaining you're same situation: https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a

Dualpurpose answered 5/7, 2018 at 10:16 Comment(6)
I have checked it but it is confusing. Q1. Do I need to create separate POJO for GSON and separate class (Entity) for ROOM? Q2. Do I need separate DAO classes for each table or single DAO can also serve?Dauphin
Q1: you can use multiple annotations in the same POJO. Q2 : yes you have to create a DAO for each table.Dualpurpose
That means I can use AutoValue with Room? I saw somewhere it is not supported as AutoValue does not genetrates setters.Dauphin
I never used AutoValue but I've just seen that you can create a Builder pattern with it.Dualpurpose
here I saw it #44870045Dauphin
Technically, this is a one-to-many relationship. "One" user has "many" pets.Glidebomb

© 2022 - 2024 — McMap. All rights reserved.