hibernate/hibernate-orm · error · IllegalArgumentException

Natural-id value [{naturalIdValue}] was not an array as expe

Error message

Natural-id value [{naturalIdValue}] was not an array as expected

What it means

Error "Natural-id value [{naturalIdValue}] was not an array as expected" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/CompoundNaturalIdMapping.java:186

	@Override
	public boolean isNormalized(Object incoming) {
		return incoming instanceof Object[];
	}

	@Override
	public void validateInternalForm(Object naturalIdValue) {
		if ( naturalIdValue != null ) {
			// should be an array, with a size equal to the number of attributes making up this compound natural-id
			if ( naturalIdValue instanceof Object[] values ) {
				if ( values.length != attributes.size() ) {
					throw new IllegalArgumentException(
							"Natural-id value [" + naturalIdValue + "] did not contain the expected number of elements ["
							+ attributes.size() + "]"
					);
				}
			}
			else {
				throw new IllegalArgumentException(
						"Natural-id value [" + naturalIdValue + "] was not an array as expected" );
			}
		}
	}

	@Override
	public int calculateHashCode(Object value) {
		if ( value == null ) {
			return 0;
		}
		else {
			final var values = (Object[]) value;
			int hashcode = 0;
			for ( int i = 0; i < attributes.size(); i++ ) {
				final Object o = values[i];
				if ( o != null ) {
					hashcode = 27 * hashcode
							+ ((JavaType) attributes.get( i ).getExpressibleJavaType()).extractHashCode( o );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Supply compound natural-id values as an array (Object[]) with one element per natural-id attribute.

When it happens

Trigger: Thrown when a compound natural-id value is passed as a scalar instead of an array of per-attribute values.

Common situations: See trigger scenarios.


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