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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With