如何在 Room 中插入具有一对多关系的实体
How to insert entities with a one to many relationship in Room
我正在使用 Room 构建数据库,但我不知道如何将具有关系(在我的例子中是一对多)的新元素插入到数据库中。没有任何解决方案曾经谈论过插入(他们只谈论查询数据)。
这是 DAO:
@Dao
abstract class ShoppingListsDao {
@Insert
abstract suspend fun addNewShoppingList(newShoppingList: ShoppingList)
@Insert
abstract suspend fun addNewItem(newItem: Item)
// This is how I thought it would work but it didn't
@Insert
@Transaction
abstract suspend fun addNewShoppingListWithItems(newShoppingListWithItems: ShoppingListWithItems)
}
这是我的实体:
@Entity
class ShoppingList(
@PrimaryKey(autoGenerate = true)
val listID: Int,
val ListName: String
)
@Entity(foreignKeys = [ForeignKey(
entity = ShoppingList::class,
parentColumns = ["listID"],
childColumns = ["parentListID"]
)])
class Item(
@PrimaryKey(autoGenerate = true)
var itemID: Int,
val name: String,
val quantity: Int,
val parentListID: Int
)
这是了解一对多关系的好资源-> https://developer.android.com/training/data-storage/room/relationships#one-to-many
您可以为 'ShoppingListWithItems' 创建嵌入对象(更多关于嵌入对象 - https://developer.android.com/training/data-storage/room/relationships#nested-objects):
data class ShoppingListWithItems(
@Embedded val shoppingList: ShoppingList,
@Relation(parentColumn = "listID", entityColumn = "parentListID") val itemList: List<Item>
)
要将它们存储在数据库中,您可以简单地使用一个事务:
@Transaction
suspend fun createTransaction(shoppingList: ShoppingList, itemList: List<Item>) {
addNewShoppingList(shoppingList)
addNewItem(*itemList) // or create an alternate to accept the list itself.
}
要检索 'ShoppingListWithItems' 实例:
@Query("SELECT * from ShoppingList where listID =:id")
suspend fun getShoppingListWithItemsById(id: String): List<ShoppingListWithItems>
据我所知,没有一种方法可以让您直接插入复合实体(例如 ShoppingListWithItems
)。您只需将各个实体插入到它们的表中。
在您的示例中,您需要为 ShoppingList
实体定义一个插入方法,其中 returns 生成主键(因此您可以将其用于其他项目)和一个插入方法对于您的 Item
实体,可以插入它们的完整列表。
@Insert
suspend fun addNewShoppingList(newShoppingList: ShoppingList): Long
@Insert
suspend fun addNewItems(newItems: List<Item>)
然后你可以运行一个事务来批量插入它们。
@Transaction
suspend fun addNewShoppingListWithItems(shoppingList: ShoppingList, items: List<Item>) {
val listId = addNewShoppingList(shoppingList)
items.forEach { it.parentListId = listId }
addNewItems(items)
}
我正在使用 Room 构建数据库,但我不知道如何将具有关系(在我的例子中是一对多)的新元素插入到数据库中。没有任何解决方案曾经谈论过插入(他们只谈论查询数据)。
这是 DAO:
@Dao
abstract class ShoppingListsDao {
@Insert
abstract suspend fun addNewShoppingList(newShoppingList: ShoppingList)
@Insert
abstract suspend fun addNewItem(newItem: Item)
// This is how I thought it would work but it didn't
@Insert
@Transaction
abstract suspend fun addNewShoppingListWithItems(newShoppingListWithItems: ShoppingListWithItems)
}
这是我的实体:
@Entity
class ShoppingList(
@PrimaryKey(autoGenerate = true)
val listID: Int,
val ListName: String
)
@Entity(foreignKeys = [ForeignKey(
entity = ShoppingList::class,
parentColumns = ["listID"],
childColumns = ["parentListID"]
)])
class Item(
@PrimaryKey(autoGenerate = true)
var itemID: Int,
val name: String,
val quantity: Int,
val parentListID: Int
)
这是了解一对多关系的好资源-> https://developer.android.com/training/data-storage/room/relationships#one-to-many
您可以为 'ShoppingListWithItems' 创建嵌入对象(更多关于嵌入对象 - https://developer.android.com/training/data-storage/room/relationships#nested-objects):
data class ShoppingListWithItems(
@Embedded val shoppingList: ShoppingList,
@Relation(parentColumn = "listID", entityColumn = "parentListID") val itemList: List<Item>
)
要将它们存储在数据库中,您可以简单地使用一个事务:
@Transaction
suspend fun createTransaction(shoppingList: ShoppingList, itemList: List<Item>) {
addNewShoppingList(shoppingList)
addNewItem(*itemList) // or create an alternate to accept the list itself.
}
要检索 'ShoppingListWithItems' 实例:
@Query("SELECT * from ShoppingList where listID =:id")
suspend fun getShoppingListWithItemsById(id: String): List<ShoppingListWithItems>
据我所知,没有一种方法可以让您直接插入复合实体(例如 ShoppingListWithItems
)。您只需将各个实体插入到它们的表中。
在您的示例中,您需要为 ShoppingList
实体定义一个插入方法,其中 returns 生成主键(因此您可以将其用于其他项目)和一个插入方法对于您的 Item
实体,可以插入它们的完整列表。
@Insert
suspend fun addNewShoppingList(newShoppingList: ShoppingList): Long
@Insert
suspend fun addNewItems(newItems: List<Item>)
然后你可以运行一个事务来批量插入它们。
@Transaction
suspend fun addNewShoppingListWithItems(shoppingList: ShoppingList, items: List<Item>) {
val listId = addNewShoppingList(shoppingList)
items.forEach { it.parentListId = listId }
addNewItems(items)
}