Dao class 必须注解@Dao - androidx.room.Dao

Dao class must be annotated with @Dao - androidx.room.Dao

我看到了这个错误列表。

1) Dao class must be annotated with @Dao - androidx.room.Dao
2) An abstract DAO method must be annotated with one and only one of the following annotations: Insert,Delete,Query,Update,RawQuery - java.lang.annotation.Annotation.equals(java.lang.Object)

其他类似第二点。

请帮助我。

我的道Class

package database
import androidx.room.*

@Dao
interface UserDao{
    //Product
    @Query("SELECT * FROM product")
     @Delete
    fun deleteProduct(product: Product)

    //Order
    @Query("SELECT * FROM `Order`")
    fun getOrderList():List<Order>
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertOrder(order: Order)
    @Delete
    fun deleteOrder(order: Order)
    ....
}

我的实体Class

package database

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class Product(
    @PrimaryKey val id:Int,
    @ColumnInfo(name = "product_name") val productName:String?,
)
@Entity
data class Order(
    @PrimaryKey val id:Int,
    @ColumnInfo(name = "client_id") val clientId:Int?,
    @ColumnInfo(name = "date") val date:String?
)
.......

我的数据库Class

package database

import androidx.room.Dao
import androidx.room.Database
import androidx.room.RoomDatabase

@Database(
    entities = [Product::class, Order::class, OrderBody::class,
        OrderToPurchase::class,Price::class,Client::class],
    version = 1
)
abstract class AppDatabase:RoomDatabase(){
    abstract fun userDao():Dao
}

你应该使用:-

abstract fun userDao():UserDao

也就是说 userDao 函数应该返回 UserDao 类型而不是 Dao 类型。

然后你会遇到问题:-

//Product
@Query("SELECT * FROM product")
 @Delete
fun deleteProduct(product: Product)

这是因为@Query没有关联函数。相反,你想要这样的东西:-

//Product
@Query("SELECT * FROM product")
fun getProductList(): List<Product> //<<<<< ADDED
@Delete
fun deleteProduct(product: Product)