hibernate/hibernate-orm · error · InstantiationException
Could not instantiate entity
Error message
Could not instantiate entity
What it means
Thrown by EmbeddableInstantiatorRecordStandard.instantiate when invoking the record's canonical constructor with the values read from the database fails. Unlike the indirecting variants, this standard instantiator passes valuesAccess.getValues() straight to the constructor, so any exception raised inside the record constructor (null validation, invalid values, arithmetic on nulls) or any reflective access problem surfaces here wrapped as InstantiationException with the original cause.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorRecordStandard.java:38
protected final Constructor<?> constructor;
public EmbeddableInstantiatorRecordStandard(Class<?> javaType) {
super( javaType );
constructor = getConstructorOrNull( javaType, getRecordComponentTypes( javaType ) );
}
@Override
public Object instantiate(ValueAccess valuesAccess) {
if ( constructor == null ) {
throw new InstantiationException( "Unable to locate constructor for embeddable", getMappedPojoClass() );
}
try {
return constructor.newInstance( valuesAccess.getValues() );
}
catch ( Exception e ) {
throw new InstantiationException( "Could not instantiate entity", getMappedPojoClass(), e );
}
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Unwrap the cause of the InstantiationException to find the exact constructor line that threw
- Relax the record constructor for values Hibernate may legitimately pass (nulls for nullable columns) or make the columns NOT NULL
- Use wrapper/object types for components backed by nullable columns
- Add 'opens <package> to hibernate.core' in module-info when entities live in a named module
Example fix
// before
public record Period(LocalDate start, LocalDate end) {
public Period { // throws on historical rows
Objects.requireNonNull(end);
}
}
// after
public record Period(LocalDate start, LocalDate end) {
public Period {
// end may be null for open-ended periods - validate at use site instead
}
} Defensive patterns
Strategy: try-catch
Validate before calling
// Detect record components that cannot handle null before going to production
for ( RecordComponent rc : Period.class.getRecordComponents() ) {
if ( rc.getType().isPrimitive() && columnAllowsNull(rc.getName()) ) {
throw new IllegalStateException("Primitive component " + rc.getName() + " on nullable column");
}
} Try / catch
try {
return session.get(Contract.class, id);
}
catch ( org.hibernate.InstantiationException e ) {
Throwable root = e.getCause() != null ? e.getCause() : e;
log.error("Record constructor for {} failed: {}", e.getMessage(), root);
throw new IllegalStateException("Stored data rejected by embeddable record", root);
} Prevention
- Keep record constructors lenient toward values the database can contain
- Push invariants to @PrePersist/@PreUpdate service-layer validation, not record constructors
- Under JPMS, open entity packages to hibernate.core to keep Constructor.newInstance legal
When it happens
Trigger: Loading an @Embedded record whose compact constructor throws on data present in the row (nulls, out-of-range values); primitive record components receiving null columns; record defined in a named module not opened to Hibernate, causing IllegalAccessException from Constructor.newInstance.
Common situations: Validation inside record constructors (requireNonNull, range checks) clashing with real production data; nullable legacy columns mapped to strictly-typed records; strict JPMS encapsulation after modularizing the domain model.
Related errors
- Could not instantiate entity
- Unable to locate constructor for embeddable
- Class '<componentClassName>' is an '@Embeddable' type and ma
- Class '<componentClassName>' is an '@Embeddable' type and ma
- Property '${path}' specifies ${columnCount} '@AttributeOverr
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/c39dc2ee9664bf89.
Report an issue: GitHub.