一对一关系的会议室数据库 [英] Room database with one-to-one relation

查看:95
本文介绍了一对一关系的会议室数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体,Coin和CoinRevenue.

I have 2 Entities, Coin and CoinRevenue.

基本上,硬币持有其他某种货币的美元价格.

Basically, coin holds the price in USD for some other currency.

例如,货币符号EUR的值为1.0356

For example, Coin with symbol EUR with value of 1.0356

@Entity(tableName = "coin")
data class Coin(
        @field:PrimaryKey(autoGenerate = false)
        var id: String = "",
        var symbol: String = "",
        var pricInUsd: Float = 0f)

CoinRevenue是一个实体,我用来保存用户拥有的特定硬币中的多少硬币. 例如,CoinRevenue与具有EUR符号且金额为1000的Coin Entity有关联.

CoinRevenue is an Entity that I use to hold how much coins of that specific coins the User have. For example, CoinRevenue has relation to Coin Entity with EUR symbol and amount of 1000.

@Entity(tableName = "coinRevenue")
    data class CoinRevenueNew(
            @field:PrimaryKey(autoGenerate = true)
            var id: Int = 0,
            var coin: Coin? = null,
            var amount: Float = 0f)

现在,我想从数据库中获取CoinRevenue,并从数据库中获取更新的Coin.

Now I want to fetch CoinRevenue from the database and get the updated Coin from the database.

例如,我将硬币保存为(EUR,1.0253) 然后用该硬币保存了CoinRevenue.

for example, i saved the Coin with (EUR,1.0253) and than Saved a CoinRevenue with that coin.

之后,我用(EUR,2.522)更新了硬币 我希望CoinRevenue中的Coin对象也将被更新.

After that I updated the Coin with (EUR,2.522) I want that the Coin object inside CoinRevenue will be updated as well.

我知道@Embedded只是将内部对象字段作为列添加到同一父对象. 当我使用关系时,我必须使用列表或集合. 但是我的CoinRevenue里总是有1个Coin.

I understand that @Embedded just add the inner objet fields as colums to the same parent object. and when I use relation, I have to use a List or a Set. but I always have 1 Coin inside CoinRevenue.

我的coinDAO:

@Query("select * from coin order by rank")
fun getAllCoins(): Flowable<List<CoinDB>>

@Query("select * from coin where rank = 1")
fun getFirstCoin(): Maybe<CoinDB>

@Query("select * from coin where favourite = 1 order by rank")
fun getAllFavouriteCoins(): Flowable<List<CoinDB>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertCoin(coinDB: CoinDB)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertCoins(coinsList: List<CoinDB>)

// -----------------
// CoinRevenue
// -----------------

@Query("select * from coinRevenue order by rank")
fun getAllCoinsRevenue(): Flowable<List<CoinRevenue>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertCoinRevenue(coinRevenue: CoinRevenue)

@Delete()
fun deleteCoinRevenue(coinRevenue: CoinRevenue)

创建此内容的最佳方法是什么?

What is the best way to creat this?

推荐答案

因此,在尝试了很多之后,我设法使其正常运行.

So after a lot of tries, I've managed to get it working.

我更改了CoinRevenue对象以持有Coin ID的外键

I Changed the CoinRevenue object to hold a foreign key to the Coin id

@Entity(tableName = "coinRevenue", foreignKeys = (arrayOf(ForeignKey(
        entity = CoinDB::class,
        onUpdate = ForeignKey.CASCADE,
        parentColumns = arrayOf("coinId"),
        childColumns = arrayOf("coinDbId"))))
)
data class CoinRevenue(
        @ColumnInfo(name = "mid")
        @PrimaryKey(autoGenerate = true)
        var id: Long = 0L,
        @ColumnInfo(name = "coinDbId")
        var coinDbId: String? = null,
        @ColumnInfo(name = "amount")
        var amount: Double = 0.toDouble()
)

我需要用两个对象创建一个POJO,

I needed to create a POJO with both objects, like that:

class CoinRevenueWithCoin() : Parcelable {
@Embedded lateinit var coinDB: CoinDB
@Embedded lateinit var coinRevenue: CoinRevenue
}

并进行如下查询:

@Query("select * from coinRevenue, coin where coinRevenue.coinDbId = coin.coinId order by coin.rank")
fun getAllCoinsRevenueWithCoin(): Flowable<List<CoinRevenueWithCoin>>

就是这样.

此外,此查询与其他常规对象查询一样,在"coin"表或"coinRevenue"表中发生任何更改时也会发出对象

In addition this query, as any other regular objects query, emit objects if there is any change in the 'coin' table or the 'coinRevenue' table

这篇关于一对一关系的会议室数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆