使用 Room Persistence Library 插入多个表
Insert into multiple tables using Room Persistence Library
我是第一次使用 Room。我有一个 class 叫:
@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;
}
我希望这两个在数据库中是分开的 table,因此我不能使用 @Embedded 注释,因为它会使用所有字段作为列创建一个 table。我也使用上面的用户 class 来存储来自服务器的 json 响应,我在 json 对象中获取用户和班次详细信息。
有什么方法可以在 shifts table 中插入班次详细信息,只要我在 users[=] 中插入用户详细信息25=] table ?我最初认为这将使用 @Embeded 来处理,但这将在 user table 中创建 shift table 列我不想。
有人可以帮助我了解我应该如何在 Room Persistence Library 中处理这个问题。类似的我也必须为删除做。
谢谢
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 ?
创建您自己的 DAO @Transaction
方法,该方法调用 User
和 Shift
的插入。
Similar I will have to do for delete as well.
如果您使用适当的级联删除选项修复 Shift
class,使其与 User
具有 @ForeignKey
关系,则删除 User
也将删除它的 Shift
行。在您当前的实施中,User
和 Shift
是不相关的。
我是第一次使用 Room。我有一个 class 叫:
@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;
}
我希望这两个在数据库中是分开的 table,因此我不能使用 @Embedded 注释,因为它会使用所有字段作为列创建一个 table。我也使用上面的用户 class 来存储来自服务器的 json 响应,我在 json 对象中获取用户和班次详细信息。
有什么方法可以在 shifts table 中插入班次详细信息,只要我在 users[=] 中插入用户详细信息25=] table ?我最初认为这将使用 @Embeded 来处理,但这将在 user table 中创建 shift table 列我不想。
有人可以帮助我了解我应该如何在 Room Persistence Library 中处理这个问题。类似的我也必须为删除做。
谢谢
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 ?
创建您自己的 DAO @Transaction
方法,该方法调用 User
和 Shift
的插入。
Similar I will have to do for delete as well.
如果您使用适当的级联删除选项修复 Shift
class,使其与 User
具有 @ForeignKey
关系,则删除 User
也将删除它的 Shift
行。在您当前的实施中,User
和 Shift
是不相关的。