Reputation: 22526
I have following interface where I have created the standard crud methods and annotated the methods with insert, update, and delete.
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>
}
I then create a interface for the 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>
}
When I compile I get a lot of error like this following:
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()
Because when I inherited from the interface. I have to manually add the @Insert, @Update, and @Delete.
Just wondering why these annontations are added automatically in the my WeatherDao interface.
So I now have to manually add them like this:
@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>
}
Just wondering if I am using this wrong:
Upvotes: 1
Views: 744
Reputation: 1183
Following this google repo, you are not doing the abstractation correctly. To sum up, you do not need to have the inserts/updates/deletes in your @Dao Interface and it should be abstract.
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>
}
Upvotes: 1