hibernate/hibernate-orm · error · NoSuchElementException

No more item in JSON document

Error message

No more item in JSON document

What it means

OsonDocumentReader wraps Oracle's OracleJsonParser for binary OSON documents. next() defensively re-checks parser.hasNext() and throws NoSuchElementException 'No more item in JSON document' when the caller drains past the end - in Hibernate-driven reads this means the embeddable mapping's structure expectation (driven by getAdapter) wants more events than the stored document actually contains.

Solutions

  1. When driving the reader directly, guard every next() with hasNext()
  2. Validate the stored documents (Oracle IS JSON check / jsonb validation, or a Jackson parse) and repair malformed rows
  3. Align the embeddable mapping with the actual document structure - missing keys are fine (nulls), but structural tokens must match
  4. Catch NoSuchElementException during aggregate reads and rethrow as a data-quality error identifying the row

Example fix

// before
while (true) {
    JsonDocumentItemType item = reader.next(); // throws 'No more item in JSON document' at end
}

// after
while (reader.hasNext()) {
    JsonDocumentItemType item = reader.next();
}
Defensive patterns

Strategy: try-catch

Validate before calling

while (reader.hasNext()) { // guard every next() when driving the reader directly
    JsonDocumentItemType item = reader.next();
}

Try / catch

try {
    MyAgg agg = session.find(MyEntity.class, id).getAgg();
} catch (NoSuchElementException e) {
    throw new DataIntegrityViolationException("Truncated/malformed JSON document in row " + id, e);
}

Prevention

When it happens

Trigger: Reading an Oracle JSON aggregate column whose stored document is truncated or shaped differently from the mapping (fewer object/array entries than the embeddable expects), so the adapter calls next() after the parser is exhausted; also direct API misuse (next() without hasNext()).

Common situations: Corrupt or hand-edited JSON/OSON data; documents written by other services with a different schema version; mapping changes across deployments; Oracle OSON round-trips with mixed tooling.

Related errors


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/type/format/OsonDocumentReader.java:48

	private Object currentValue;

	/**
	 * Creates a new <code>OsonDocumentReader</code>  on top of a <code>OracleJsonParser</code>
	 * @param parser the parser
	 */
	public OsonDocumentReader(OracleJsonParser parser) {
		this.parser = parser;
	}

	@Override
	public boolean hasNext() {
		return this.parser.hasNext();
	}

	@Override
	public JsonDocumentItemType next() {
		if (!this.parser.hasNext())
			throw new NoSuchElementException("No more item in JSON document");
		OracleJsonParser.Event evt = this.parser.next();
		currentKeyName = null;
		currentValue = null;
		switch (evt) {
			case START_OBJECT:
				return JsonDocumentItemType.OBJECT_START;
			case END_OBJECT:
				return JsonDocumentItemType.OBJECT_END;
			case START_ARRAY:
				return JsonDocumentItemType.ARRAY_START;
			case END_ARRAY:
				return JsonDocumentItemType.ARRAY_END;
			case KEY_NAME:
				currentKeyName = this.parser.getString();
				return JsonDocumentItemType.VALUE_KEY;
			case VALUE_TIMESTAMPTZ:
				currentValue = this.parser.getOffsetDateTime();
				return JsonDocumentItemType.VALUE;

View on GitHub (pinned to fad1729dce)