greenrobot/greenDAO · error · DaoException

No entity found for query

Error message

No entity found for query

What it means

Query.uniqueOrThrow() calls unique() and throws a DaoException if no row matched the query, distinguishing 'no result' (exception) from the safe unique() which just returns null. The library throws this so callers that require an entity can get a fail-fast error instead of a NullPointerException later.

Solutions

  1. Use unique() instead of uniqueOrThrow() and handle the null return.
  2. Check existence first (e.g. queryBuilder...count() or a select on the key) before uniqueOrThrow().
  3. Wrap in try-catch for DaoException when absence is an expected business case.

Example fix

// before
User u = queryBuilder.where(UserDao.Properties.Email.eq(email)).uniqueOrThrow();
// after
User u = queryBuilder.where(UserDao.Properties.Email.eq(email)).unique();
if (u == null) { /* handle missing user */ }
Defensive patterns

Strategy: try-catch

Validate before calling

long count = queryBuilder.where(UserDao.Properties.Email.eq(email)).count();
boolean exists = count > 0;

Try / catch

try { u = q.uniqueOrThrow(); } catch (DaoException e) { /* create default or report not-found */ }

Prevention

When it happens

Trigger: Calling uniqueOrThrow() on a Query whose WHERE condition matched zero rows — e.g. queryBuilder.where(Properties.Id.eq(missingId)).uniqueOrThrow().

Common situations: Lookups by primary key or unique column where the key came from user input, an external API, or a stale reference to a deleted row.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08). Data as JSON: /api/errors/9c39c6ea5522be73. Report an issue: GitHub.

Appendix: source

Thrown at DaoCore/src/main/java/org/greenrobot/greendao/query/Query.java:142

     * @return Entity or null if no matching entity was found
     * @throws DaoException if the result is not unique
     */
    public T unique() {
        checkThread();
        Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
        return daoAccess.loadUniqueAndCloseCursor(cursor);
    }

    /**
     * Executes the query and returns the unique result (never null).
     *
     * @return Entity
     * @throws DaoException if the result is not unique or no entity was found
     */
    public T uniqueOrThrow() {
        T entity = unique();
        if (entity == null) {
            throw new DaoException("No entity found for query");
        }
        return entity;
    }

    @Override
    public Query<T> setParameter(int index, Object parameter) {
        return (Query<T>) super.setParameter(index, parameter);
    }

    @Override
    public Query<T> setParameter(int index, Date parameter) {
        return (Query<T>) super.setParameter(index, parameter);
    }

    @Override
    public Query<T> setParameter(int index, Boolean parameter) {
        return (Query<T>) super.setParameter(index, parameter);
    }

View on GitHub (pinned to 0bbb338e17)