2

how to relate user table "addressId" column with address table "id" column? and when i insert user model data then how can i insert data in both tables "user and address". and same case in fetching data?

Address model is nested class for user model.

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

    @PrimaryKey(autoGenerate = true)
    private Integer localId;

    private Integer id;

    private String name;

    private Integer addressId;

    private Address address;
}

@Entity(tableName = "address")
public class Address {

    @PrimaryKey(autoGenerate = true)
    private Integer localId;

    private Integer id;

    private String address1;

    private String address2;

    private String address3;
}
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
vinay
  • 21
  • 2
  • Refer this https://mobikul.com/saving-complex-objects-using-android-room-library/ – Jaykishan Sewak Mar 19 '19 at 13:18
  • Possible duplicate of [Android room persistent library - how to insert class that has a List object field](https://stackoverflow.com/questions/44580702/android-room-persistent-library-how-to-insert-class-that-has-a-list-object-fie) – nishon.tan Mar 21 '19 at 18:05

1 Answers1

0

See https://developer.android.com/training/data-storage/room/referencing-data

Basically your options are:

  • restructure to use different tables
  • use @Embedded
  • using type converter save them in some primitive types (may restrict your ablity to query on these data)
Arka Prava Basu
  • 2,366
  • 3
  • 18
  • 34