hibernate/hibernate-orm · error · IllegalArgumentException

page number must be non-negative

Error message

page number must be non-negative

What it means

Constructor precondition on Page: the page number is 0-based and must be non-negative, since a negative number would produce an invalid (negative) first-result offset via size*number. The faulty input is the number argument passed to the Page constructor or page(n) factory.

Solutions

  1. Pass a page number of zero or greater.
  2. Validate pagination input from the caller.
Defensive patterns

Strategy: validation

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/d4285eae265aa5a6. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/query/Page.java:93

	 */
	public int getMaxResults() {
		return size;
	}

	/**
	 * @return the {@linkplain SelectionQuery#getFirstResult()
	 *         offset} of this page.
	 */
	public int getFirstResult() {
		return size*number;
	}

	Page(int size, int number) {
		if ( size <= 0 ) {
			throw new IllegalArgumentException("page size must be strictly positive");
		}
		if ( number < 0 ) {
			throw new IllegalArgumentException("page number must be non-negative");
		}
		this.size = size;
		this.number = number;
	}

	/**
	 * Obtain a page, numbered from {@code 0}, with the given size.
	 * @param size the number of results on the initial page.
	 * @param number the number of the page, where {@code 0} is the first page.
	 */
	public static Page page(int size, int number) {
		return new Page( size, number );
	}

	/**
	 * Obtain an initial page with the given size.
	 * @param size the number of results on the initial page.
	 */

View on GitHub (pinned to fad1729dce)