hibernate/hibernate-orm · error · InstantiationException
Could not instantiate entity
Error message
Could not instantiate entity
What it means
Thrown by EntityInstantiatorPojoStandard.instantiate() when invoking the previously resolved default constructor throws. The class is concrete and the no-arg constructor exists, but constructor.newInstance((Object[]) null) fails - either the constructor body itself raises an exception, or reflective access is denied (non-public constructor/class in a package not accessible to Hibernate, Java 9+ strong encapsulation). The root cause is chained onto the InstantiationException.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java:101
return super.isInstance( object )
// this one needed only for guessEntityMode()
|| proxyInterface != null && proxyInterface.isInstance( object );
}
@Override
public Object instantiate() {
if ( isAbstract() ) {
throw new InstantiationException( "Cannot instantiate abstract class or interface", getMappedPojoClass() );
}
else if ( constructor == null ) {
throw new InstantiationException( "No default constructor for entity", getMappedPojoClass() );
}
else {
try {
return applyInterception( constructor.newInstance( (Object[]) null ) );
}
catch ( Exception e ) {
throw new InstantiationException( "Could not instantiate entity", getMappedPojoClass(), e );
}
}
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Inspect getCause() - IllegalAccessException means an access problem, anything else points at the constructor body
- Make the no-arg constructor and the entity class at least package-visible to Hibernate, and add 'opens <entity.package> to hibernate.core' under JPMS
- Move initialization logic that can throw out of the default constructor into @PostLoad/@PrePersist callbacks or factories
- Verify no classpath/classloader split puts a stale (non-public-ctor) version of the class in front of Hibernate
Example fix
// before - default ctor does environment-dependent work
@Entity
public class Report {
protected Report() {
this.generatedAt = ClockHolder.clock().instant(); // throws if holder unset
}
}
// after - keep the ctor trivial
@Entity
public class Report {
protected Report() { }
@PrePersist
void stamp() { this.generatedAt = Instant.now(); }
} Defensive patterns
Strategy: try-catch
Validate before calling
// Startup: verify the default ctor is callable in the runtime environment
for ( EntityType<?> t : emf.getMetamodel().getEntities() ) {
Class<?> c = t.getJavaType();
if ( c != null && !c.isInterface() && !Modifier.isAbstract(c.getModifiers()) ) {
try { c.getDeclaredConstructor().newInstance(); }
catch (ReflectiveOperationException e) { throw new IllegalStateException("Entity default ctor not invocable: " + c, e); }
}
} Try / catch
try {
return session.find(Order.class, id);
}
catch ( org.hibernate.InstantiationException e ) {
Throwable root = e.getCause() != null ? e.getCause() : e;
if ( root instanceof IllegalAccessException ) {
throw new IllegalStateException("Entity constructor not accessible - check visibility/JPMS opens", root);
}
throw new IllegalStateException("Entity default constructor threw", root);
} Prevention
- Keep entity no-arg constructors trivial - no I/O, service lookups, or throwing initializers
- Under JPMS, 'opens <entity.package> to hibernate.core' in module-info.java
- Smoke-test one em.find per entity in CI to execute default constructors in a real environment
When it happens
Trigger: A default constructor that throws on its implicit defaults (NPE initializing fields from statics, failing time sources); package-private or private no-arg constructors in named modules without 'opens'; entity class non-public and the constructor not accessible from hibernate-core; constructors performing I/O or context lookups that fail in the persistence environment.
Common situations: Service-locator or clock calls inside default constructors that break inside Hibernate threads; JPMS modularized applications where entity packages are not opened to hibernate.core; exotic classloader setups (OSGi, war/ear redeploys) where accessibility checks fail intermittently.
Related errors
- The {} class [{}] could not be instantiated
- Unable to generate set of all hints - {}
- Cannot instantiate abstract class or interface
- Unable to create AttributeConverter instance
- Error processing @TypeBinderType annotation '%s' for entity
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/68b2a750f0b5d2d9.
Report an issue: GitHub.