gpt4 book ai didi

android - 房间持久性库事务

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:28 25 4
gpt4 key购买 nike

我有 2 个实体类:

@Entity(tableName = "work_orders")
public class WorkOrderEntity
{
@PrimaryKey(autoGenerate = true)
private long id;
...
}

@Entity(tableName = "activity_logs", foreignKeys = {
@ForeignKey(entity = WorkOrderEntity.class, parentColumns = "id", childColumns = "work_order_id") })
public class ActivityLogEntity
{
@PrimaryKey(autoGenerate = true)
private long id;

@ColumnInfo(name = "work_order_id")
private long workOrderId;
...
}

有没有办法在一个事务中在 work_orders 和 activity_log 表中插入记录?像这样:

@Transaction
public void insertBoth(WorkOrderEntity workOrderEntity, ActivityLogEntity activityLogEntity)
{
insertWorkOrder(workOrderEntity);
insertActivityLog(activityLogEntity);
}

牢记外键约束。

这里是类似的问题Insert into multiple tables using Room Persistence Library , 但没有详尽的答案

最佳答案

我想出了这个解决方案:

@Transaction
public void insertBoth(WorkOrderEntity workOrderEntity, ActivityLogEntity activityLogEntity)
{
long workOrderId = insertWorkOrder(workOrderEntity);
activityLogEntity.setWorkOrderId(workOrderId);
insertActivityLog(activityLogEntity);
}

关于android - 房间持久性库事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46935293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com