Android Room数据库:如何嵌入多个实体

时间:2018-08-15 14:36:22

标签: database kotlin persistence android-room

我正在为新项目使用空间,但是我在为如何干净地访问一对一值而苦苦挣扎
我有地址,所有者和站点的实体,所有者和站点都有一个关联的地址,并且每个站点都有一个所有者
是否可以干净地访问这些文件,例如site.owner.address.l1

我调查了@Relation,尽管它返回的列表不理想

实体

@Entity(tableName="addresses")
data class Address(
    @PrimaryKey
    @ColumnInfo(name="address_id")
    val id: Int,

    @ColumnInfo(name="address_l1")
    val l1: String
)

@Entity(tablename="owners")
data class Owner(
    @PrimaryKey
    @ColumnInfo(name="owner_id")
    val id: Int,

    @ColumnInfo(name="owner_name")
    val name: String
)

@Entity(tableName="sites")
data class Site(
    @PrimaryKey
    @ColumnInfo(name="site_id")
    val id: Int,

    @ColumnInfo(name="site_addressid")
    val addressid: Int

    @ColumnInfo(name="site_ownerid")
    val ownerid: Int
)

道的

@Query("SELECT * FROM sites")
fun getSites(): List<Site>

@Query("SELECT * FROM owners")
fun getOwners(): List<Owner>

@Query("SELECT * FROM addresses")
fun getAddresses(): List<Address>

@Query("SELECT * FROM sites JOIN address ON sites.site_addressid = address.address_id JOIN owners ON sites.site_ownerid = owners.owner_id JOIN address ON owners.owner_addressid = address.address_id WHERE sites.site_id = :siteid")
fun getSiteByIdWithDetails(siteid): Site

我只能看到多个POJO的方法来完成这项工作,例如
无效但大致正确

data class OwnerAddress(
    @Embedded
    val owner: Owner,

    @Embedded
    val address: Address
)

data class SiteAddress(
    @Embedded
    val site: Site,

    @Embedded
    val owner: OwnerAddress,

    @Embedded
    val address: Address
}

并通过

使用它
@Query("SELECT * FROM sites JOIN address ON sites.site_addressid = address.address_id JOIN owners ON sites.site_ownerid = owners.owner_id JOIN address ON owners.owner_addressid = address.address_id WHERE sites.site_id = :siteid")
fun getSiteByIdWithDetails(siteid): SiteAddress

2 个答案:

答案 0 :(得分:0)

您是否尝试过在实体之间建立关系? Android Documentation for Relationships

@Entity(tableName="addresses")
data class Address(
    @PrimaryKey
    @ColumnInfo(name="address_id")
    val id: Int,

    @ColumnInfo(name="address_l1")
    val l1: String
)

@Entity(tablename="owners")
data class Owner(
    @PrimaryKey
    @ColumnInfo(name="owner_id")
    val id: Int,

    @ColumnInfo(name="owner_name")
    val name: String

    @ColumnInfo(name="owner_adress")
    @Entity val adress: Address
)

@Entity(tableName="sites")
data class Site(
    @PrimaryKey
    @ColumnInfo(name="site_id")
    val id: Int,

    @ColumnInfo(name="site_owner")
    @Embedded val owner: Owner

    @ColumnInfo(name="site_address")
    @Embedded val address: Address
)

因此,现在您的站点有一个地址和一个所有者(有其地址)。 sites表和owners表都将包含“地址”列。

答案 1 :(得分:0)

您可以为您的范围使用 TypeConverter 和 Gson 库。

将您的数据类定义为

@Entity(tableName="addresses")
data class Address(
    @PrimaryKey
    @ColumnInfo(name="address_id")
    val id: Int,

    @ColumnInfo(name="address_l1")
    val l1: String
)

@Entity(tablename="owners")
data class Owner(
    @PrimaryKey
    @ColumnInfo(name="owner_id")
    val id: Int,

    @ColumnInfo(name="owner_name")
    val name: String
)

@Entity(tableName="sites")
data class Site(
    @PrimaryKey
    @ColumnInfo(name="site_id")
    val id: Int,

    @ColumnInfo(name="site_address")
    val address: Address,

    @ColumnInfo(name="site_owner")
    val owner: Owner
)

然后创建一个 DatabaseConverter

class DatabaseConverter 
{
    @TypeConverter
    fun ownerFromJson(jsonString: String) : Owner {
        val ownerType = object : TypeToken<Owner>() { }.type
        return Gson().fromJson(jsonString, ownerType)
    }

    @TypeConverter
    fun jsonFromOwner(owner: Owner) : String = Gson().toJson(owner)

    @TypeConverter
    fun addressFromJson(jsonString: String) : Address {
        val addressType = object : TypeToken<Address>() { }.type
        return Gson().fromJson(jsonString, addressType)
    }

    @TypeConverter
    fun jsonFromAddress(address: Address) : String = Gson().toJson(address)
}

然后将其附加到您的 RoomDatabase 实例类

@Database(entities = [ ... ], version = ...)
@TypeConverters(DatabaseConverter::class)
abstract class YourDatabaseClass: RoomDatabase() {
    ...
}

您现在应该可以使用

访问您的子实体实例
site.site_address.id 
site.site_owner.id