Reputation: 1302
It's the first time I am using Room. I have a class called:
@Entity(tableName = "users")
class User{
@PrimaryKey
@ColumnInfo(name = "id")
@SerializedName("id")
String id;
@ColumnInfo(name = "name")
@SerializedName("name")
String name;
@SerializedName("shift")
@Ignore
List<Shift> shifts;
}
@Entity(tableName = "shifts")
class Shift{
@PrimaryKey
@ColumnInfo(name = "id")
@SerializedName("id")
String id;
@ColumnInfo(name = "start_time")
@SerializedName("start_time")
String startTime;
@ColumnInfo(name = "end_time")
@SerializedName("end_time")
String endTime;
}
I want these two to be seperate tables in the database, hence I cannot user @Embedded annotation, as it will create a single table using all field as columns. I am using the above User class to store json reponse from the server as well, where I get the user and shift details information in a json object.
Is there any way I can insert the Shift details in shifts table as soon as I insert the User details in the users table ? I initially thought this will be handled using @Embeded but that will create shift table columns in the user table which I do not want.
Can someone help me as to how I am suppose to handle this in Room Persistence Library. Similar I will have to do for delete as well.
Thanks
Upvotes: 6
Views: 9040
Reputation: 1006584
Is there any way I can insert the Shift details in shifts table as soon as I insert the User details in the users table ?
Create your own DAO @Transaction
method that calls inserts for User
and Shift
.
Similar I will have to do for delete as well.
If you fix your Shift
class, such that it has a @ForeignKey
relationship to User
with the appropriate cascade-delete option, deleting a User
will delete its Shift
rows as well. In your current implementation, User
and Shift
are unrelated.
Upvotes: 3