hibernate/hibernate-orm · error · IllegalArgumentException

Value to extract hashCode from cannot be null

Error message

Value to extract hashCode from cannot be null

What it means

JavaType.extractHashCode(T) is a default interface method that rejects null with IllegalArgumentException because there is no meaningful hash for a null domain value, then delegates to value.hashCode(). Hitting it means some Hibernate component asked the JavaType for the hash of a null — most commonly a custom JavaType/UserType that kept the default implementation and receives nulls during dirty checking or hash-based key computation.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaType.java:227

	 * Retrieve the natural comparator for this type.
	 */
	default Comparator<T> getComparator() {
		//noinspection unchecked
		return Comparable.class.isAssignableFrom( getJavaTypeClass() )
				? ComparableComparator.INSTANCE
				: null;
	}

	/**
	 * Extract a proper hash code for the given value.
	 *
	 * @param value The value for which to extract a hash code.
	 *
	 * @return The extracted hash code.
	 */
	default int extractHashCode(T value) {
		if ( value == null ) {
			throw new IllegalArgumentException( "Value to extract hashCode from cannot be null" );
		}
		return value.hashCode();
	}

	/**
	 * Determine if two instances are equal
	 *
	 * @param one One instance
	 * @param another The other instance
	 *
	 * @return True if the two are considered equal; false otherwise.
	 */
	default boolean areEqual(T one, T another) {
		return Objects.deepEquals( one, another );
	}

	/**
	 * Whether to use {@link Object#equals(Object)} and {@link Object#hashCode()}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Override extractHashCode in your custom JavaType with a null-safe policy (e.g. return 0 for null)
  2. Make areEqual and extractHashCode consistently null-aware so nulls never reach the default method
  3. Check the stack trace to see which component (dirty checker, cache key, collection) passed null
  4. If nulls are legitimate for the mapped data, fix the mapping so hash positions cannot be null

Example fix

// before: default method throws on null
@Override
public int extractHashCode(MyType value) { // inherited default
    ...
}

// after: null-safe override in your custom type
@Override
public int extractHashCode(MyType value) {
    return value == null ? 0 : value.hashCode();
}
Defensive patterns

Strategy: validation

Validate before calling

// before any manual hash use of a JavaType
if (value == null) {
    // skip hashing; Hibernate's own areEqual path handles null comparisons
}

Type guard

static boolean hashable(Object v) { return v != null; }

Prevention

When it happens

Trigger: Custom JavaType implementations (registered via @JavaType/@Type) that do not override extractHashCode, used on nullable attributes where null reaches hash computation; null values in id or map-key positions typed with the custom type; wrapper types feeding null through.

Common situations: Introducing a custom type for a nullable column; null embedded-id components; collections compared or keyed through the custom type; Hibernate upgrades where hash-based dirty checking paths started calling this method.

Related errors


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