greenrobot/greenDAO · error · DaoException
Entity does not exist in the database anymore
Error message
Entity does not exist in the database anymore: ${entity.getClass()} with key ${key} What it means
AbstractDao.load(Key) (and refresh-style paths) re-selects the row by key to (re)populate the passed entity. If no row matches the key, greenDAO throws DaoException because the entity is considered deleted. The message identifies the entity class and key.
Solutions
- Check existence first with count(...)/queryBuilder...unique() before calling load().
- Catch DaoException and treat it as 'entity deleted' in caller logic.
- Re-query the current list instead of relying on a stale key.
- Verify you are connected to the same database instance/file where the row was written.
Example fix
// before
User u = userDao.load(userId); // throws if deleted
// after
User u = userDao.queryBuilder().where(UserDao.Properties.Id.eq(userId)).unique();
if (u == null) { /* handle deleted entity */ } Defensive patterns
Strategy: try-catch
Validate before calling
boolean exists = userDao.queryBuilder().where(Properties.Id.eq(key)).buildCount().count() > 0;
Try / catch
try {
User u = userDao.load(key);
} catch (DaoException e) {
// treat as deleted/stale reference
u = null;
} Prevention
- Treat held ids as weak references; re-validate before load.
- Catch DaoException on loads that can race with deletes.
- Avoid caching keys across database wipes/reinstalls.
- Use unique() queries returning null instead of load(key) when absence is expected.
When it happens
Trigger: Calling load(key) or load(entity)/refresh paths with a key that no longer exists in the table — the row was deleted by another session/process, the DB was recreated, or the key is stale/wrong type.
Common situations: Holding an id across a database wipe (e.g. app data cleared, in-memory test DB); deleting the row in a transaction while another thread still holds the key; a stale cache referencing deleted rows.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Expected unique result, but count was
- Cannot delete entity, key is null
- Cannot update entity without key - was it inserted before?
- ( ) does not have a single-column primary key
- Entity may not be null
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/7a022ec454e62acc.
Report an issue: GitHub.
Appendix: source
Thrown at DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java:755
* Deletes all entities with the given keys in the database using a transaction.
*
* @param keys Keys of the entities to delete.
*/
public void deleteByKeyInTx(K... keys) {
deleteInTxInternal(null, Arrays.asList(keys));
}
/** Resets all locally changed properties of the entity by reloading the values from the database. */
public void refresh(T entity) {
assertSinglePk();
K key = getKeyVerified(entity);
String sql = statements.getSelectByKey();
String[] keyArray = new String[]{key.toString()};
Cursor cursor = db.rawQuery(sql, keyArray);
try {
boolean available = cursor.moveToFirst();
if (!available) {
throw new DaoException("Entity does not exist in the database anymore: " + entity.getClass()
+ " with key " + key);
} else if (!cursor.isLast()) {
throw new DaoException("Expected unique result, but count was " + cursor.getCount());
}
readEntity(cursor, entity, 0);
attachEntity(key, entity, true);
} finally {
cursor.close();
}
}
public void update(T entity) {
assertSinglePk();
DatabaseStatement stmt = statements.getUpdateStatement();
if (db.isDbLockedByCurrentThread()) {
synchronized (stmt) {
if (isStandardSQLite) {
updateInsideSynchronized(entity, (SQLiteStatement) stmt.getRawStatement(), true);View on GitHub (pinned to 0bbb338e17)