【问题标题】:Android Room Kotlin inner joinAndroid Room Kotlin 内连接
【发布时间】:2018-03-15 17:25:59
【问题描述】:

我有两个实体 帐户:

@Entity(tableName = "accounts",foreignKeys = arrayOf(
        ForeignKey(
                entity = Currency::class,
                parentColumns = arrayOf("id"),
                childColumns = arrayOf("currencyId"),
                onDelete = ForeignKey.CASCADE
        )
))
data class Account (
        @PrimaryKey(autoGenerate = true)
        var id:Int=0,
        @ColumnInfo(name="name")
        var accountName:String,
        @ColumnInfo(name = "balance")
        var initialBalance:Double,
        var currencyId:Int,
        var date:Date,
        var isDefault:Boolean=true
){
    constructor():this(0,"",0.0,0,Date(),false)
}

还有货币:

@Entity(tableName = "currencies")
data class Currency(
        @PrimaryKey(autoGenerate = true)
        var id:Int=0,
        @ColumnInfo(name="name")
        var currencyName:String,
        @ColumnInfo(name="code")
        var currencyCode:String
)
{
    constructor():this(0,"","")

    override fun toString(): String =currencyCode
}

我想在account 中嵌入一个currency 对象。如您所见,currenciesaccounts 之间存在一对多关系。当我查询accounts 实体时,我也想查看它的货币。 我尝试在account 实体中添加@Embedded 字段,但它不起作用显然我误解了一些东西,该字段返回null“没有例外只是null”。如果可以将currency 对象“展平”在account 对象内,那就更好了。

所有这一切的重点是,我想显示RecyclerView 中的所有帐户及其货币信息。我现在在 @Embedded@Relation 之间感到困惑,任何帮助将不胜感激。

编辑
我不知道这是否有帮助:
这是我的AccountDao:

@Dao
interface AccountDao {
    @Insert
    fun insertAll(items:Array<Account>)

    @Update
    fun update(item:Account)

    @Delete
    fun delete(item:Account)

    @Query("select * from accounts")
    fun getAll():LiveData<Array<Account>>
}

【问题讨论】:

    标签: android kotlin android-room


    【解决方案1】:

    我不会推荐上述方法,因为您最终会编写相同的属性(重复自己),也就是样板代码。

    按以下方式使用@EmbeddedRelation 注释,您的代码很可能如下所示:

    data class AccountWithCurrency (
        @Embedded
        var account: Account? = null,
        @Relation(parentColumn = "id", entityColumn = "currencyId")
        var currency: List<Currency>? = null,
    ){
    constructor() : this(Account(), emptyList())
    }
    

    【讨论】:

    • 感谢您的贡献,但我已经提到“如果可以“扁平化”帐户对象内的货币对象,这会好得多。那对我来说更好。无论如何,+1 告诉我如何正确使用 @Relation@Embedded
    • 如果在加入表格后过滤记录,这将不起作用
    【解决方案2】:

    我设法让它工作。解决方案是创建一个单独的(POJO 或 POKO 等)类,我称之为 AccountModel

    class AccountModel{
        var accountId:Int = 0
        var accountName:String = ""
        var accountInitialBalance:Double = 0.0
        var accountCreationDate: Date = Date()
        var currencyId:Int = 0
        var currencyCode:String = ""
        var isDefaultAccount:Boolean = false
        constructor()
        constructor(id:Int,name:String,balance:Double,date:Date,currencyId:Int,currencyCode:String,isDefault:Boolean){
            this.accountId = id
            this.accountName = name
            this.accountInitialBalance = balance
            this.accountCreationDate = date
            this.currencyId = currencyId
            this.currencyCode = currencyCode
            this.isDefaultAccount= isDefault
        }
        fun toAccount():Account = Account(this.accountId,this.accountName,this.accountInitialBalance,this.currencyId,this.accountCreationDate,this.isDefaultAccount)
    }
    

    然后,构造查询以执行普通的inner join,就好像您正在为普通的SQL 数据库执行inner join。像这样:

    @Query("select accounts.id as accountId," +
                "accounts.name as accountName," +
                "accounts.balance as accountInitialBalance," +
                "accounts.currencyId," +
                "accounts.date as accountCreationDate," +
                "accounts.isDefault as isDefaultAccount," +
                "currencies.code as currencyCode " +
                "from accounts inner join currencies on accounts.currencyId=currencies.id")
        fun getAll():LiveData<Array<AccountModel>>
    

    显然,您可以使用as x 将此列投影到返回对象中的x 字段,如您所知,在数据库中该列是accounts.id,但在我的AccountModel 中它是一个@ 987654330@.
    Google Room 真正令人印象深刻的是,即使我添加了一个非常聪明的Account 对象,我也能够获得LiveDataAccountModel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      相关资源
      最近更新 更多