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

  1. Add a no-arg constructor to the entity (public or protected), e.g. Lombok @NoArgsConstructor or a protected no-op constructor
  2. For Kotlin, apply the kotlin.plugin.jpa / no-arg plugin so the compiler synthesizes the constructor
  3. 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

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


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