hibernate/hibernate-orm · error · InstantiationException
Cannot instantiate abstract class or interface
Error message
Cannot instantiate abstract class or interface
What it means
Thrown by EntityInstantiatorPojoStandard.instantiate() when Hibernate is asked to create an instance of an entity whose mapped class is abstract (or an interface). The instantiator deliberately skips constructor resolution for abstract classes (constructor = null when isAbstract()), so any runtime path that requires a concrete instance of the abstract mapped class itself - rather than one of its mapped subclasses - fails with this InstantiationException.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java:91
null,
null
) );
}
return entity;
}
@Override
public boolean isInstance(Object object) {
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
- If the base type should never be instantiated, map it as @MappedSuperclass instead of @Entity
- Ensure every discriminator value stored in the database has a concrete @Entity subclass mapping
- Query/load concrete subclass types rather than the abstract root where instantiation is implied
- If the class must stay a mapped @Entity but is abstract, add a concrete subclass covering the rows in question
Example fix
// before
@Entity
@Inheritance(strategy = SINGLE_TABLE)
public abstract class Payment { ... } // rows exist for unknown discriminator values
// after
@MappedSuperclass // not an entity itself
public abstract class PaymentBase { ... }
@Entity
@Inheritance(strategy = SINGLE_TABLE)
public abstract class Payment extends PaymentBase { ... } // plus concrete CardPayment/CashPayment for every discriminator value Defensive patterns
Strategy: validation
Validate before calling
// Startup guard: no mapped @Entity class should be abstract AND instantiable via root loads
for ( EntityType<?> t : emf.getMetamodel().getEntities() ) {
if ( Modifier.isAbstract( t.getJavaType().getModifiers() ) && isRootEntity(t) && !hasMappedSubclassesForAllDiscriminators(t) ) {
log.warn("Abstract mapped entity {} may receive unresolvable rows - consider @MappedSuperclass", t.getName());
}
} Type guard
static boolean isConcrete(Class<?> c) { return !Modifier.isAbstract(c.getModifiers()); } Try / catch
try {
return em.find(Payment.class, id);
}
catch ( org.hibernate.InstantiationException e ) {
if ( "Cannot instantiate abstract class or interface".equals(e.getMessage()) ) {
throw new IllegalStateException("Row resolves to abstract root " + e.getMessage() + " - map missing subclass or use @MappedSuperclass", e);
}
throw e;
} Prevention
- Map never-instantiated bases as @MappedSuperclass, not @Entity
- Keep discriminator values in data synchronized with mapped subclasses; add migration inserts for new types
- Review each SINGLE_TABLE hierarchy after adding abstract layers or renaming subclasses
When it happens
Trigger: Loading a row whose discriminator/subclass resolution lands on the abstract root entity (e.g., a discriminator value mapped to the abstract class or missing subclass mappings); session.refresh/instantiate directly against an abstract entity name; polymorphic references (to-one associations typed as the abstract root) where the target resolves to the root entity itself.
Common situations: Mapping an abstract base class with @Entity and forgetting @MappedSuperclass; adding a new discriminator value in the database without mapping the corresponding subclass; queries like em.find(AbstractBase.class, id) on hierarchies where the row belongs to an unmapped/abstract type.
Related errors
- Could not instantiate entity
- Root entity '<entityName>' is annotated '@DiscriminatorOptio
- Class '<className>' is not the root class of an entity inher
- Mapped superclass '{}' may not specify an '@Inheritance' map
- Entity '{}' may not override the inheritance mapping strateg
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/994c9503efbdc5c7.
Report an issue: GitHub.