mybatis/mybatis-3 · error · ReflectionException
There is no default constructor for {}
Error message
There is no default constructor for {} What it means
Reflector.getDefaultConstructor() throws when the reflected class has no public no-arg constructor. MyBatis instantiates result types, parameter types, and typeHandler-owning classes through this path, and without a default constructor it cannot create the instance.
Source
Thrown at src/main/java/org/apache/ibatis/reflection/Reflector.java:371
return false;
}
return true;
}
/**
* Gets the name of the class the instance provides information for.
*
* @return The class name
*/
public Class<?> getType() {
return clazz;
}
public Constructor<?> getDefaultConstructor() {
if (defaultConstructor != null) {
return defaultConstructor;
}
throw new ReflectionException("There is no default constructor for " + clazz);
}
public boolean hasDefaultConstructor() {
return defaultConstructor != null;
}
public Invoker getSetInvoker(String propertyName) {
Invoker method = setMethods.get(propertyName);
if (method == null) {
throw new ReflectionException("There is no setter for property named '" + propertyName + "' in '" + clazz + "'");
}
return method;
}
public Invoker getGetInvoker(String propertyName) {
Invoker method = getMethods.get(propertyName);
if (method == null) {
throw new ReflectionException("There is no getter for property named '" + propertyName + "' in '" + clazz + "'");View on GitHub (pinned to 008069adb1)
Solutions
- Add a public no-arg constructor to the class (plus @NoArgsConstructor alongside Lombok @AllArgsConstructor).
- Map through a constructor instead: <constructor> elements in the resultMap, or argNameBasedConstructorAutomapping/autoMappingBehavior, so MyBatis uses the arg-taking constructor.
- For immutable objects, use a custom ObjectFactory or a TypeHandler/ResultHandler that builds the object yourself.
Example fix
// before
@AllArgsConstructor
public class User { private final Long id; }
// after
@NoArgsConstructor
@AllArgsConstructor
public class User { private Long id; } Defensive patterns
Strategy: validation
Validate before calling
boolean instantiable = new ReflectorFactory().createForType(User.class).hasDefaultConstructor();
if (!instantiable) { /* add no-arg ctor or map via <constructor> */ } Prevention
- Give every result/parameter DTO a no-arg constructor (or Lombok @NoArgsConstructor) by convention.
- For immutable classes, plan <constructor> mappings from the start instead of setter-based automapping.
When it happens
Trigger: A resultType/parameterType class (or javaType handled by a factory) whose only constructors take arguments, reached via ObjectFactory.create(type) during result mapping or parameter processing.
Common situations: DTOs with only an all-args constructor (common with Lombok @AllArgsConstructor without @NoArgsConstructor); Kotlin data classes without a no-arg constructor (or kotlin-noarg plugin); immutable value objects.
Related errors
- Error instantiating {} with invalid types ({}) or values ({}
- Cannot set value of property '{}' because '{}' is null and c
- Unable to find a usable constructor for {}
- Error creating instance. Cause: {cause}
- Error in result map '{resultMapId}'. Failed to find a constr
AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14).
Data as JSON: /api/errors/36af1def0f0e16c8.
Report an issue: GitHub.