从房间 DAO 接口继承



我有以下界面,在其中创建了标准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。

只是想知道为什么这些公告会自动添加到我的天气道界面中。

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

@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>
}

只是想知道我是否用错了这个:

遵循此谷歌存储库,您没有正确进行抽象。总而言之,您不需要在@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>
}

最新更新