我有两个实体,Coin和CoinRevenue。
基本上,coin保存了某种其他货币的美元价格。
例如,带有符号EUR且价值为1.0356的Coin。
@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(tableName = "coinRevenue")
data class CoinRevenueNew(
@field:PrimaryKey(autoGenerate = true)
var id: Int = 0,
var coin: Coin? = null,
var amount: Float = 0f)
现在我想从数据库中获取CoinRevenue并从数据库中获取更新后的Coin。
例如,我保存了币种为(EUR,1.0253)的Coin,然后保存了一个带有该货币的CoinRevenue。
之后,我更新了具有(EUR,2.522)的Coin,我希望CoinRevenue内部的Coin对象也会更新。
我理解@Embedded只是将内部对象字段作为列添加到同一父对象中。当我使用关系时,我必须使用List或Set,但我始终只有一个Coin在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)
如何最好地创建这个?