Android Room错误: room Entity class must be annotated with @Entity

  • 使用Room过程中报错:room Entity class must be annotated with @Entity,不同情况可能会不同,仅记录一下自己碰到该错误产生的原因。
//@Database(entities = {CurrentTeacherInfo.class,TeacherLogDao.class}, version = 1)				//使用该行代码,就会产生上面的错误。
@Database(entities = {CurrentTeacherInfo.class,TeacherLog.class}, version = 1)						//使用该行代码,没有错误。
public abstract class UserDatabase extends RoomDatabase {

    //数据库名称
    private static final String DB_NAME = "xxx.db";
    private static volatile UserDatabase instance;
    
    public static synchronized UserDatabase getInstance(Context context) {
        if (instance == null) {
            instance = create(context);
        }
        return instance;
    }
    
    private static UserDatabase create(final Context context) {
        return Room.databaseBuilder(
                context,
                UserDatabase.class,
                DB_NAME)
                .allowMainThreadQueries()
                .build();
    }

    //RoomDatabase提供直接访问底层数据库实现,我们通过定义抽象方法返回具体Dao
    //然后进行数据库增删该查的实现。

    //xxx信息
    public abstract CurrentTeacherInfoDao getCurrentTeacherInfoDao();

    //xxx日志
    public abstract TeacherLogDao getTeacherLogDao();

}
  • 想必已经看到了区别,那么我们看一下TeacherLogDao和TeacherLog类:
//------------------------------------------------------------------------------ TeacherLogDao 类---------------------------------------------------------------------
@Dao
public interface TeacherLogDao {

    @Query("SELECT * FROM TeacherLog")
    List<TeacherLog> getAll();

    /**
     * 分页查找数据
     * @param pos      开始查找的位置
     * @param count    一次查询的数量
     * @return
     */
    @Query("SELECT * FROM TeacherLog LIMIT (:pos),(:count)")
    List<TeacherLog> getTeacherLog(int pos,int count);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    Long insert(TeacherLog teacherLog);
}

//----------------------------------------------------------------------------- TeacherLog 类----------------------------------------------------------------------
@Entity(tableName = "teacherLog")
public class TeacherLog {

    //自增长
    @PrimaryKey(autoGenerate=true)
    private long uid;

    private String date;
    private String time;
    private String title;
    private String content;

    public long getUid() {
        return uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "TeacherLog{" +
                "uid=" + uid +
                ", date='" + date + '\'' +
                ", time='" + time + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}
  • 原因:错误产生的原因已经很明显了。
  • 将@Database(entities = {CurrentTeacherInfo.class,TeacherLogDao.class}, version = 1) 中的TeacherLogDao.class改成TeacherLog.class即可。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值