- 使用Room过程中报错:room Entity class must be annotated with @Entity,不同情况可能会不同,仅记录一下自己碰到该错误产生的原因。
@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();
}
public abstract CurrentTeacherInfoDao getCurrentTeacherInfoDao();
public abstract TeacherLogDao getTeacherLogDao();
}
- 想必已经看到了区别,那么我们看一下TeacherLogDao和TeacherLog类:
@Dao
public interface TeacherLogDao {
@Query("SELECT * FROM TeacherLog")
List<TeacherLog> getAll();
@Query("SELECT * FROM TeacherLog LIMIT (:pos),(:count)")
List<TeacherLog> getTeacherLog(int pos,int count);
@Insert(onConflict = OnConflictStrategy.REPLACE)
Long insert(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即可。