“Type of the parameter must be a class annotated with @Entity” while creating Generic DAO interface in Room

后端 未结 4 765
鱼传尺愫
鱼传尺愫 2020-12-16 19:52

I am using Room architecture component for persistence. I have created generic DAO interface to avoid boilerplate code. Room Pro Tips

But my code doesn\'t compile s

4条回答
  •  攒了一身酷
    2020-12-16 20:12

    I had initially followed the method used in Kotlin, but that gives the error in Java code. Two quick changes fixed it for me

    • Change BaseDao to Abstract class
    • Added @Dao annotation to the BaseDao

    Please find the code below and now it runs properly

    @Dao
    abstract class BaseDao {
    
       @Insert(onConflict = OnConflictStrategy.REPLACE)
       abstract void insert(T entity);
    
       @Update
       abstract void update(T entity);
    
       @Delete
       abstract void delete(T entity);
     }
    
     @Dao
     public abstract class ReasonDao extends BaseDao{
    
        @Query("SELECT * from Reason")
        abstract public List getReasons();
    
      }
    

提交回复
热议问题