如何解决如果我使用autoGenerate = true,则带有Room Android的@Update表数据库会生成新ID
- 我的问题是,当我致电
appDatabase.studentDAO().update(student);
时,记录没有更新。 (student
模型是由database
从id
进行查询的,当我在那里调试时,它仍然很好) - 我认为是由于
autoGenerate = true
的{{1}}中的标志@PrimaryKey id
,因为当我授予该标志StudentEntity
时,它已成功更新。 - 即使我使用
false
(而不是true
),即使我保留该标记@Update
,它也会生成一个新的id
我的实体:
@Insert
我的TypeConverter:
@Entity(tableName = "StudentTable")
public class StudentEntity {
@PrimaryKey(autoGenerate = true)
private int id;
private String firstName;
private String lastName;
private int genderId;
private int classId;
private ArrayList<TranscriptModel> transcriptList;
}
我的数据库:
public class GsonUtils {
@TypeConverter
public static ArrayList<TranscriptModel> getListTranscriptFromString(String transcriptString) {
Type transcriptType = new TypeToken<ArrayList<TranscriptModel>>() {
}.getType();
return new Gson().fromJson(transcriptString,transcriptType);
}
@TypeConverter
public static String listTranscriptToString(ArrayList<TranscriptModel> transcriptList) {
Gson gson = new Gson();
return gson.toJson(transcriptList);
}
}
我的DAO:
@Database(version = 1,entities = {StudentEntity.class})
@TypeConverters({GsonUtils.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract StudentDAO studentDAO();
public static AppDatabase getInstance(Context context) {
if (INSTANCE == null) {
synchronized (AppDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,Constants.DATABASE_NAME)
.allowMainThreadQueries()
.build();
}
}
}
return INSTANCE;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。