【问题标题】:Inheriting from a room DAO interface从房间 DAO 接口继承
【发布时间】:2020-05-12 15:51:03
【问题描述】:

我有以下界面,我在其中创建了标准的 crud 方法,并使用插入、更新和删除对方法进行了注释。

interface BaseDao<T> {
    @Insert
    fun insert(table: T): Single<Long>

    @Insert
    fun insert(vararg table: T): Single<List<Long>>

    @Update
    fun update(table: T): Single<Int>

    @Delete
    fun delete(table: T): Single<Int>
}

然后我为 DAO 创建一个接口

@Dao
interface WeatherDao : BaseDao<WeatherTable> {

    override fun insert(table: WeatherTable): Single<Long>

    override fun insert(vararg table: WeatherTable): Single<List<Long>>

    override fun update(table: WeatherTable): Single<Int>

    override fun delete(table: WeatherTable): Single<Int>

    @Query("SELECT * FROM weatherTable")
    fun getAllWeather(): Single<List<WeatherTable>>

    @Query("SELECT * FROM weatherTable WHERE id = :id LIMIT 1")
    fun getWeatherById(id: Long): Single<WeatherTable>

    @Query("SELECT count(*) FROM weatherTable")
    fun count(): Single<Int>
}

当我编译时,我收到很多如下错误:

error: An abstract DAO method must be annotated with one and only one of the following annotations: Insert,Delete,Query,Update,RawQuery
    public abstract io.reactivex.Single<java.lang.Long> delete(@org.jetbrains.annotations.NotNull()

因为当我从接口继承。我必须手动添加@Insert、@Update 和@Delete。

只是想知道为什么这些注释会自动添加到我的 WeatherDao 界面中。

所以我现在必须像这样手动添加它们:

@Dao
interface WeatherDao : BaseDao<WeatherTable> {

    @Insert
    override fun insert(table: WeatherTable): Single<Long>

    @Insert
    override fun insert(vararg table: WeatherTable): Single<List<Long>>

    @Update
    override fun update(table: WeatherTable): Single<Int>

    @Delete
    override fun delete(table: WeatherTable): Single<Int>

    @Query("SELECT * FROM weatherTable")
    fun getAllWeather(): Single<List<WeatherTable>>

    @Query("SELECT * FROM weatherTable WHERE id = :id LIMIT 1")
    fun getWeatherById(id: Long): Single<WeatherTable>

    @Query("SELECT count(*) FROM weatherTable")
    fun count(): Single<Int>
}

只是想知道我是不是用错了:

【问题讨论】:

  • 为什么要覆盖WeatherDaoBaseDao的crud方法?通过删除它们,它应该可以解决!

标签: android android-room


【解决方案1】:

按照这个google repo,你没有正确地进行抽象。总而言之,您的@Dao 接口中不需要插入/更新/删除,它应该是抽象的。

interface BaseDao<T> {

    /**
     * Insert an object in the database.
     *
     * @param obj the object to be inserted.
     */
    @Insert
    fun insert(obj: T)

    /**
     * Insert an array of objects in the database.
     *
     * @param obj the objects to be inserted.
     */
    @Insert
    fun insert(vararg obj: T)

    /**
     * Update an object from the database.
     *
     * @param obj the object to be updated
     */
    @Update
    fun update(obj: T)

    /**
     * Delete an object from the database
     *
     * @param obj the object to be deleted
     */
    @Delete
    fun delete(obj: T)
}
@Entity(tableName = "data")
data class Data(@PrimaryKey val id: String, val value: String)
@Dao
abstract class DataDao : BaseDao<Data>() {

    /**
     * Get all data from the Data table.
     */
    @Query("SELECT * FROM Data")
    abstract fun getData(): List<Data>
}

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多