Room API-如何获取实体最近插入的生成ID? [英] Room API - How to retrieve recently inserted generated id of the entity?
本文介绍了Room API-如何获取实体最近插入的生成ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检索最近插入的实体的生成的id
值,如下面添加的代码示例所示:
I need to retrieve the generated id
value of an entity which is recently inserted as the code sample added below demonstrates:
如上所述,实体Student
的id
字段用PrimaryKey(autoGenerate= true)
注释.
As mentioned above, the id
field of the entity Student
is annotated with PrimaryKey(autoGenerate= true)
.
Student s = new Student();
s.setName("foo");
// call of other setters
appDatabase.getGenericDAO().insertStudent(s);
// Now I need to retrieve the value of generated id as we do in Hibernate by the usage of merge method
Integer id = s.getId();
DAO
@Dao
public interface IGenericDAO {
@Insert
Long insertStudent(Student student);
@Insert
void insertOgrenci(List<Student> studentList);
@Update
void updateOgrenci(Ogrenci... ogrenci);
@Delete
void deleteOgrenci(Ogrenci... ogrenci);
}
推荐答案
让您的@Insert
方法返回long
:
@Insert
long insertStudent(Student s);
返回值将是该行的rowId
,并且应该是自动生成的主键值.
The return value will be the rowId
of the row, and that should be the auto-generated primary key value.
这篇关于Room API-如何获取实体最近插入的生成ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文