{"record":{"id":"0c3ce34d840946a2","repo":"hibernate/hibernate-orm","slug":"cannot-instantiate-the-class-because-it-does","errorCode":null,"errorMessage":"Cannot instantiate the class [{}] because it does not have a constructor that accepts a dialect or an empty constructor","messagePattern":"Cannot instantiate the class \\[(.+?)\\] because it does not have a constructor that accepts a dialect or an empty constructor","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java","lineNumber":733,"sourceCode":"\n\t\t\t\t\tif ( entityBasedConstructor == null ) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tif ( dialectConstructor != null ) {\n\t\t\t\t\t\t\t\treturn dialectConstructor.newInstance(\n\t\t\t\t\t\t\t\t\t\tserviceRegistry.requireService( JdbcServices.class ).getDialect()\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if ( emptyConstructor != null ) {\n\t\t\t\t\t\t\t\treturn emptyConstructor.newInstance();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcatch (Exception e) {\n\t\t\t\t\t\t\tthrow new StrategySelectionException(\n\t\t\t\t\t\t\t\t\t\"Could not instantiate named strategy class [\" + strategyClass.getName() + \"]\",\n\t\t\t\t\t\t\t\t\te\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow new IllegalArgumentException( \"Cannot instantiate the class [\" + strategyClass.getName()\n\t\t\t\t\t\t\t\t+ \"] because it does not have a constructor that accepts a dialect or an empty constructor\" );\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t);\n\t}\n\n\t@SuppressWarnings(\"unchecked\")\n\t@Nullable\n\tprivate Constructor<SqmMultiTableMutationStrategy> resolveSqmMutationStrategyConstructor(\n\t\t\tString strategyName,\n\t\t\tStrategySelector strategySelector) {\n\t\tif ( strategyName != null ) {\n\t\t\tfinal var strategyClass =\n\t\t\t\t\tstrategySelector.selectStrategyImplementor( SqmMultiTableMutationStrategy.class, strategyName );\n\t\t\tfor ( var declaredConstructor : strategyClass.getDeclaredConstructors() ) {","sourceCodeStart":715,"sourceCodeEnd":751,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java#L715-L751","documentation":"For a custom class named by hibernate.query.mutation_strategy, SessionFactoryOptionsBuilder scans declared constructors looking for one of two supported shapes: a single-argument Dialect constructor, or a no-arg constructor (a (EntityMappingType, RuntimeModelCreationContext) constructor defers instantiation per entity). If none of these exists, it throws IllegalArgumentException explaining the class cannot be instantiated. This is a constructor-shape mismatch, not an invocation failure.","triggerScenarios":"Registering a SqmMultiTableMutationStrategy implementation that only offers constructors with other parameter lists, e.g. MyStrategy(ConnectionProvider), MyStrategy(int), or MyStrategy(Dialect, Settings).","commonSituations":"Writing a custom strategy from scratch without matching Hibernate's expected constructor contract; refactoring a working strategy's constructor signature; strategy originally built for constructor injection frameworks.","solutions":["Add a public constructor taking exactly one org.hibernate.dialect.Dialect argument","Or add a public no-argument constructor","Or provide a public constructor with exactly (EntityMappingType, RuntimeModelCreationContext) for per-entity strategies","Keep at least one of the supported constructors when refactoring"],"exampleFix":"// before:\npublic MyMutationStrategy(ConnectionProvider cp) { ... } // only constructor\n\n// after:\npublic MyMutationStrategy(Dialect dialect) {\n    this.dialect = dialect;\n    this.cp = null; // resolve lazily if needed\n}\npublic MyMutationStrategy(ConnectionProvider cp) { ... } // keep for your own use","handlingStrategy":"validation","validationCode":"// fail fast if the custom strategy lacks a supported constructor\nClass<?> strategy = Class.forName(props.getProperty(\"hibernate.query.mutation_strategy\"));\nboolean ok = Arrays.stream(strategy.getDeclaredConstructors()).anyMatch(c -> {\n    Class<?>[] p = c.getParameterTypes();\n    return p.length == 0 || Dialect.class == p[0]\n        || (p.length == 2 && p[0] == EntityMappingType.class && p[1] == RuntimeModelCreationContext.class);\n});\nif (!ok) throw new IllegalStateException(\"Strategy needs (Dialect), (), or (EntityMappingType, RuntimeModelCreationContext) ctor\");","typeGuard":"static boolean hasHibernateStrategyConstructor(Class<?> c) {\n    return Arrays.stream(c.getDeclaredConstructors()).anyMatch(ctor -> {\n        Class<?>[] p = ctor.getParameterTypes();\n        return p.length == 0 || (p.length == 1 && Dialect.class.isAssignableFrom(p[0]))\n            || (p.length == 2 && p[0] == EntityMappingType.class\n                && p[1] == RuntimeModelCreationContext.class);\n    });\n}","tryCatchPattern":"try {\n    sessionFactory = metadata.buildSessionFactory();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"does not have a constructor\")) {\n        // add a (Dialect) or no-arg constructor to the named class, then rebuild\n    }\n    throw e;\n}","preventionTips":["Treat the supported constructor shapes ((Dialect), (), (EntityMappingType, RuntimeModelCreationContext)) as part of the strategy SPI contract and document them in the class javadoc","Add an architecture/reflect test asserting the strategy keeps a supported constructor","Prefer the Dialect constructor so strategies can adapt per database"],"tags":["hibernate","strategy","constructor","mutation-strategy","configuration"],"backgroundTag":"missing-required-constructor","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}