2017-10-25 5 views
2

私は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; 
    ... 
} 

を1つのトランザクションでwork_ordersとactivity_logテーブルにレコードを挿入する方法はありますか?このようなもの:

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

外部キー制約を念頭に置いてください。

ここ

は、同様の質問Insert into multiple tables using Room Persistence Libraryですが、精緻な答え

+0

'@ Transaction'アノテーションは存在するので、あなたの' insertBothは() 'はかなり問題ないはずです'insertBoth()'、 'insertWorkOrder()'、 'insertActivityLog()'はあなたの '@ Dao'クラスのすべてのメソッドであると書かれています。 – CommonsWare

+0

はい、 'insertBoth()'、 'insertWorkOrder()'と 'insertActivityLog()'を同じ '@ Dao'クラスに入れましたが、問題は、自動生成されたIDを持つWorkOrderEntityを挿入した後、そのIDがActivityLogEntity' workOrderId' –

答えて

2

せずに私はこの解決策を考え出す:

@Transaction 
public void insertBoth(WorkOrderEntity workOrderEntity, ActivityLogEntity activityLogEntity) 
{ 
    long workOrderId = insertWorkOrder(workOrderEntity); 
    activityLogEntity.setWorkOrderId(workOrderId); 
    insertActivityLog(activityLogEntity); 
} 
関連する問題