hibernate/hibernate-orm · error · InstantiationException
No default constructor for entity
Error message
No default constructor for entity
What it means
Thrown by EntityInstantiatorPojoStandard.instantiate() when the mapped POJO entity class has no no-arg constructor. During instantiator construction Hibernate calls ReflectHelper.getDefaultConstructor(mappedPojoClass); on PropertyNotFoundException it logs 'no default constructor' via CORE_LOGGER and stores null, so the failure surfaces at runtime the first time Hibernate must create an entity instance itself (loading, persisting a new instance, refresh of detached data).
Source
Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java:94
}
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
- Add a no-arg constructor to the entity (public or protected), e.g. Lombok @NoArgsConstructor or a protected no-op constructor
- For Kotlin, apply the kotlin.plugin.jpa / no-arg plugin so the compiler synthesizes the constructor
- Keep argument constructors for business use but delegate to defaults from the no-arg one
Example fix
// before
@Entity
public class Order {
private final String number; // only ctor takes args
public Order(String number) { this.number = number; }
}
// after
@Entity
public class Order {
private String number;
protected Order() { } // for Hibernate
public Order(String number) { this.number = number; }
} Defensive patterns
Strategy: validation
Validate before calling
// Fail fast if any mapped entity lacks a no-arg ctor
for ( EntityType<?> t : emf.getMetamodel().getEntities() ) {
Class<?> c = t.getJavaType();
if ( c != null ) {
try { c.getDeclaredConstructor(); }
catch (NoSuchMethodException e) { throw new IllegalStateException("Entity " + c + " has no no-arg constructor"); }
}
} Type guard
static boolean hasNoArgConstructor(Class<?> c) {
try { c.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return false; }
} Try / catch
try {
session.persist(newEntity);
}
catch ( org.hibernate.InstantiationException e ) {
if ( "No default constructor for entity".equals(e.getMessage()) ) {
throw new IllegalStateException("Add a no-arg constructor to " + e.getMessage(), e);
}
throw e;
} Prevention
- Give every @Entity a public/protected no-arg constructor (JPA requirement)
- Kotlin: enable kotlin("plugin.jpa") / all-open; Lombok: pair @Builder with @NoArgsConstructor
- Add an architecture test (ArchUnit) asserting a no-arg constructor on all @Entity classes
When it happens
Trigger: An @Entity class whose only constructors take arguments; persist() (not merge()) of a new instance when Hibernate still needs the default constructor for interception/enhancement; loading rows via em.find or queries for entities lacking a no-arg constructor.
Common situations: Kotlin data/regular classes without the kotlin-noarg or all-open plugin (JPA plugin); Lombok classes using @Builder/@AllArgsConstructor without @NoArgsConstructor; Java value-style entities with only canonical constructors; constructors made private for factory patterns.
Related errors
- Error processing @TypeBinderType annotation '%s' for entity
- Cannot instantiate abstract class or interface
- Could not instantiate entity
- Could not resolve PropertyAccess for attribute `%s#%s`
- Class '<componentClassName>' is an '@Embeddable' type and ma
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/c39b97c54cb283cc.
Report an issue: GitHub.