quarkusio/quarkus · error · PanacheQueryException

There should be only one result

Error message

There should be only one result

What it means

Panache's singleResult() expects exactly one row from the executed MongoDB query. The library fetches up to 2 rows (list(2)) and throws PanacheQueryException if the result count is not exactly 1 — i.e. zero rows or more than one row both trigger it.

Source

Thrown at extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java:225

    @SuppressWarnings("unchecked")
    public <T extends Entity> Stream<T> stream() {
        return (Stream<T>) list().stream();
    }

    public <T extends Entity> T firstResult() {
        List<T> list = list(1);
        return list.isEmpty() ? null : list.get(0);
    }

    public <T extends Entity> Optional<T> firstResultOptional() {
        return Optional.ofNullable(firstResult());
    }

    public <T extends Entity> T singleResult() {
        List<T> list = list(2);
        if (list.size() != 1) {
            throw new PanacheQueryException("There should be only one result");
        }

        return list.get(0);
    }

    public <T extends Entity> Optional<T> singleResultOptional() {
        List<T> list = list(2);
        if (list.size() > 1) {
            throw new PanacheQueryException("There should be no more than one result");
        }

        return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
    }

    private void manageOffsets(FindIterable find, Integer limit) {
        if (range != null) {
            find.skip(range.getStartIndex());
            if (limit == null) {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Check the query matches exactly one document; add/fix the filter before calling singleResult().
  2. If zero rows is acceptable, use singleResultOptional() instead.
  3. If only the first row is needed regardless of count, use firstResult().
  4. Delete duplicate documents in the collection or add an application-level uniqueness check.

Example fix

// before
MyEntity e = query.singleResult(); // throws when 0 or >1 rows
// after
Optional<MyEntity> e = query.singleResultOptional(); // tolerates 0 rows
// or
MyEntity e = query.firstResult(); // takes first row only
Defensive patterns

Strategy: try-catch

Validate before calling

long count = query.count();
if (count != 1) {
    // handle zero/multiple rows before calling singleResult()
}

Try / catch

try {
    MyEntity e = query.singleResult();
} catch (PanacheQueryException e) {
    // fall back to firstResult() or Optional handling
}

Prevention

When it happens

Trigger: Calling query.singleResult() when the query matches no documents, or when the query (filter/sort) matches two or more documents.

Common situations: Forgetting a filter so an entity query matches every document; unique constraints assumed but not enforced in MongoDB; stale data left from earlier test runs.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/398db3a0df98e463. Report an issue: GitHub.