{"record":{"id":"19d426965ebb97e8","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-named-strategy-class-s-19d426","errorCode":null,"errorMessage":"Could not instantiate named strategy class [%s]","messagePattern":"Could not instantiate named strategy class \\[(.+?)\\]","errorType":"exception","errorClass":"StrategySelectionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorImpl.java","lineNumber":181,"sourceCode":"\t\t\t\treturn defaultResolver.call();\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new StrategySelectionException( \"Default resolver threw exception\", e );\n\t\t\t}\n\t\t}\n\t\telse if ( strategy.isInstance( strategyReference ) ) {\n\t\t\treturn strategy.cast( strategyReference );\n\t\t}\n\t\telse {\n\t\t\tfinal var implementationClass =\n\t\t\t\t\tstrategyReference instanceof Class\n\t\t\t\t\t\t\t? (Class<? extends T>) strategyReference\n\t\t\t\t\t\t\t: selectStrategyImplementor( strategy, strategyReference.toString() );\n\t\t\ttry {\n\t\t\t\treturn creator.create( implementationClass );\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new StrategySelectionException(\n\t\t\t\t\t\tString.format( \"Could not instantiate named strategy class [%s]\",\n\t\t\t\t\t\t\t\timplementationClass.getName() ),\n\t\t\t\t\t\te\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static <T> T create(Class<T> strategyClass) {\n\t\ttry {\n\t\t\treturn strategyClass.getDeclaredConstructor().newInstance();\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new StrategySelectionException(\n\t\t\t\t\tString.format( \"Could not instantiate named strategy class [%s]\", strategyClass.getName() ),\n\t\t\t\t\te\n\t\t\t);\n\t\t}","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorImpl.java#L163-L199","documentation":"When a strategy reference is non-null and not already an instance of the strategy, resolveStrategy resolves the implementation class (from the reference or selectStrategyImplementor) and hands it to the caller-supplied StrategyCreator; any exception from creator.create() is wrapped as StrategySelectionException 'Could not instantiate named strategy class [%s]'. The named class resolved fine - creating the instance failed.","triggerScenarios":"A strategy setting naming a class whose instantiation throws: a missing or inaccessible constructor, a constructor that fails (missing configuration, NPE), or a static initializer error - for custom dialects, ConnectionProviders, RegionFactories, JTA platforms, etc.","commonSituations":"Custom strategy classes without usable constructors; constructors reading configuration that is absent at creation time; classes whose static initialization touches unavailable resources; constructor injection designed for frameworks, not for reflective instantiation by Hibernate.","solutions":["Ensure the strategy class has a usable public no-arg constructor","Read the wrapped cause (getCause()) - it carries the actual construction failure; fix that underlying problem","If the class needs arguments, instantiate it yourself and pass the instance (many settings accept an instance as well as a class/name)","Supply a custom StrategyCreator that constructs the object with the arguments it needs"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify a public no-arg constructor exists before accepting a strategy class\nConstructor<?> c = implClass.getDeclaredConstructor();\nif (!Modifier.isPublic(c.getModifiers()) || !Modifier.isPublic(implClass.getModifiers())) {\n    throw new IllegalStateException(\n        implClass.getName() + \" needs a public no-arg constructor for Hibernate instantiation\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return creator.create(implementationClass);\n} catch (StrategySelectionException e) {\n    // getCause() carries the constructor/static-init failure - fix that, not this wrapper\n    throw new ConfigurationException(\"Cannot create \" + implementationClass.getName(), e.getCause());\n}","preventionTips":["Give every Hibernate-instantiated strategy class a public no-arg constructor","Keep constructors cheap; move setup into start/configure lifecycle callbacks","Pass pre-built instances where APIs accept them instead of class names"],"tags":["hibernate","strategy","instantiation","configuration","reflection"],"backgroundTag":"class-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}