ACgamerDC
ACgamerDC

Reputation: 13

How to handle Object References in Entity?

I have the following structure to save to app database:

@Entity
public class Project{
   @primaryKey
   String id;
   String name;
   [...]
   Country country;
   [...]
}

And my Country Entity looks like the following:

@Entity
public class Country {
    @PrimaryKey
    private String id;
    private String name;
    private String pk;
}

Now to my Question: How do I make Room know the Relation between Country and Project Entity?

Upvotes: 1

Views: 824

Answers (2)

diegoveloper
diegoveloper

Reputation: 103451

Please refer the official documentation

https://developer.android.com/reference/android/arch/persistence/room/Relation.html

Upvotes: 1

Rohan Arora
Rohan Arora

Reputation: 661

Room can not have nested entities, you can embedd POJO classes in an entity but it will get flattened into a single table or if you want Country as an entity then you'll have to store county_id in Project entity and index it as foreign key.

More on Embedded fields: https://developer.android.com/reference/android/arch/persistence/room/Embedded.html

More on Foreign key: https://developer.android.com/reference/android/arch/persistence/room/ForeignKey.html

Upvotes: 1

Related Questions