我需要获取最近插入的实体的生成id
值,如下所示的代码示例:
如上所述,实体Student
的id
字段带有注释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()
返回的内容。 - CommonsWareinsert()
返回SQLite通过sqlite3_last_insert_rowid()
公开的值。引用这些文档,“大多数SQLite表中的每个条目(除了WITHOUT ROWID表)都有一个名为“rowid”的唯一64位有符号整数键。只要这些名称没有被显式声明的列使用,ROWID、OID或_ROWID_作为未声明的列名始终可用。如果表具有INTEGER PRIMARY KEY类型的列,则该列是rowid的另一个别名。” - CommonsWare