Room API - 如何检索最近插入的实体生成的id?

时间:2018-04-20 21:57:24

标签: android sqlite android-sqlite android-room

我需要检索最近插入的实体的生成id值,因为下面添加的代码示例演示了:

如上所述,实体id的{​​{1}}字段已注明Student

PrimaryKey(autoGenerate= true)

DAO

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(); 

1 个答案:

答案 0 :(得分:3)

让您的@Insert方法返回long

@Insert
long insertStudent(Student s);

返回值将是行的rowId,这应该是自动生成的主键值。

相关问题