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

Room API - How to retrieve recently inserted generated id of the entity?

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

如上所述,实体Studentid字段被注解为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方法return成为long:

@Insert
long insertStudent(Student s);

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