{"record":{"id":"7a022ec454e62acc","repo":"greenrobot/greenDAO","slug":"entity-does-not-exist-in-the-database-anymore-e","errorCode":null,"errorMessage":"Entity does not exist in the database anymore: ${entity.getClass()} with key ${key}","messagePattern":"Entity does not exist in the database anymore: (.+?) with key (.+?)","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java","lineNumber":755,"sourceCode":"     * Deletes all entities with the given keys in the database using a transaction.\n     *\n     * @param keys Keys of the entities to delete.\n     */\n    public void deleteByKeyInTx(K... keys) {\n        deleteInTxInternal(null, Arrays.asList(keys));\n    }\n\n    /** Resets all locally changed properties of the entity by reloading the values from the database. */\n    public void refresh(T entity) {\n        assertSinglePk();\n        K key = getKeyVerified(entity);\n        String sql = statements.getSelectByKey();\n        String[] keyArray = new String[]{key.toString()};\n        Cursor cursor = db.rawQuery(sql, keyArray);\n        try {\n            boolean available = cursor.moveToFirst();\n            if (!available) {\n                throw new DaoException(\"Entity does not exist in the database anymore: \" + entity.getClass()\n                        + \" with key \" + key);\n            } else if (!cursor.isLast()) {\n                throw new DaoException(\"Expected unique result, but count was \" + cursor.getCount());\n            }\n            readEntity(cursor, entity, 0);\n            attachEntity(key, entity, true);\n        } finally {\n            cursor.close();\n        }\n    }\n\n    public void update(T entity) {\n        assertSinglePk();\n        DatabaseStatement stmt = statements.getUpdateStatement();\n        if (db.isDbLockedByCurrentThread()) {\n            synchronized (stmt) {\n                if (isStandardSQLite) {\n                    updateInsideSynchronized(entity, (SQLiteStatement) stmt.getRawStatement(), true);","sourceCodeStart":737,"sourceCodeEnd":773,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java#L737-L773","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nUser u = userDao.load(userId); // throws if deleted\n// after\nUser u = userDao.queryBuilder().where(UserDao.Properties.Id.eq(userId)).unique();\nif (u == null) { /* handle deleted entity */ }","handlingStrategy":"try-catch","validationCode":"boolean exists = userDao.queryBuilder().where(Properties.Id.eq(key)).buildCount().count() > 0;","typeGuard":null,"tryCatchPattern":"try {\n    User u = userDao.load(key);\n} catch (DaoException e) {\n    // treat as deleted/stale reference\n    u = null;\n}","preventionTips":["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."],"tags":["database","greendao","stale-key","deleted-entity"],"backgroundTag":"entity-not-found","analyzedSha":"0bbb338e17c4cf28aba5aae62d42f73f50186157","analyzedAt":"2026-09-08T03:22:36.050Z","contentChangedAt":"2026-09-08T03:22:36.050Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}