{"record":{"id":"18cdb86a74b1a31a","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-named-strategy-class-s","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/internal/SessionFactoryOptionsBuilder.java","lineNumber":1004,"sourceCode":"\t\t\t\t},\n\t\t\t\tcreationContext\n\t\t);\n\t}\n\n\tprivate static FormatMapper formatMapper(\n\t\t\tObject setting,\n\t\t\tStrategySelector selector,\n\t\t\tCallable<FormatMapper> defaultResolver, FormatMapperCreationContext creationContext) {\n\t\treturn selector.resolveStrategy( FormatMapper.class, setting, defaultResolver, strategyClass -> {\n\t\t\ttry {\n\t\t\t\treturn strategyClass.getDeclaredConstructor( FormatMapperCreationContext.class )\n\t\t\t\t\t\t.newInstance( creationContext );\n\t\t\t}\n\t\t\tcatch (NoSuchMethodException e) {\n\t\t\t\t// Ignore\n\t\t\t}\n\t\t\tcatch (InvocationTargetException | InstantiationException | IllegalAccessException e) {\n\t\t\t\tthrow new StrategySelectionException(\n\t\t\t\t\t\tString.format( \"Could not instantiate named strategy class [%s]\", strategyClass.getName() ),\n\t\t\t\t\t\te\n\t\t\t\t);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn strategyClass.getDeclaredConstructor().newInstance();\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]\", strategyClass.getName() ),\n\t\t\t\t\t\te\n\t\t\t\t);\n\t\t\t}\n\t\t} );\n\t}\n\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~","sourceCodeStart":986,"sourceCodeEnd":1022,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java#L986-L1022","documentation":"For hibernate.type.json_format_mapper or hibernate.type.xml_format_mapper naming a custom FormatMapper class, SessionFactoryOptionsBuilder first tries the constructor taking FormatMapperCreationContext. If that constructor exists but its invocation fails (InvocationTargetException, InstantiationException for abstract classes, or IllegalAccessException for non-public access), a StrategySelectionException is thrown during SessionFactory bootstrap.","triggerScenarios":"Setting hibernate.type.json_format_mapper=com.acme.MyFormatMapper where MyFormatMapper has a FormatMapperCreationContext constructor that is not public, the class is abstract, or the constructor body throws (e.g. failing to build an ObjectMapper).","commonSituations":"Custom Jackson/Yasson wrapper whose constructor configures an ObjectMapper that fails (module missing); constructor left package-private; subclassing a shipped FormatMapper but keeping it abstract by mistake; the context passed in being used before it is fully initialized.","solutions":["Inspect the nested cause — InvocationTargetException carries the exception your constructor threw","Make the class concrete and the FormatMapperCreationContext constructor public (getDeclaredConstructor is used, but access must still succeed)","Harden ObjectMapper/Jsonb construction: register optional modules defensively, don't throw on unknown settings","If the context constructor is not needed, delete it so Hibernate falls back to the no-arg constructor path"],"exampleFix":"// before:\nMyFormatMapper(FormatMapperCreationContext ctx) { // package-private + throws\n    this.mapper = new ObjectMapper().registerModule(new JavaTimeModule()); // JavaTimeModule not on classpath\n}\n\n// after:\npublic MyFormatMapper(FormatMapperCreationContext ctx) {\n    ObjectMapper m = new ObjectMapper();\n    if (ClassUtils.isPresent(\"com.fasterxml.jackson.datatype.jsr310.JavaTimeModule\", null)) {\n        m.registerModule(new JavaTimeModule());\n    }\n    this.mapper = m;\n}","handlingStrategy":"try-catch","validationCode":"// if you register a custom FormatMapper, check its constructors beforehand\nClass<?> c = Class.forName(props.getProperty(\"hibernate.type.json_format_mapper\"));\nboolean ctxCtor = Arrays.stream(c.getDeclaredConstructors())\n    .anyMatch(ct -> ct.getParameterCount() == 1\n                && ct.getParameterTypes()[0] == FormatMapperCreationContext.class\n                && Modifier.isPublic(ct.getModifiers()));\nboolean noArg = Arrays.stream(c.getDeclaredConstructors())\n    .anyMatch(ct -> ct.getParameterCount() == 0 && Modifier.isPublic(ct.getModifiers()));\nif (!ctxCtor && !noArg) throw new IllegalStateException(\"FormatMapper needs a usable constructor\");","typeGuard":null,"tryCatchPattern":"try {\n    sessionFactory = metadata.buildSessionFactory();\n} catch (StrategySelectionException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Could not instantiate named strategy class\")) {\n        Throwable root = e.getCause();\n        while (root != null && root.getCause() != null) root = root.getCause();\n        log.error(\"FormatMapper construction failed: {}\", root, e);\n    }\n    throw e;\n}","preventionTips":["Build ObjectMapper/Jsonb instances defensively — register optional modules only when present","Make the FormatMapper class public and concrete","Test mapper construction directly in a unit test separate from Hibernate boot"],"tags":["hibernate","format-mapper","json","xml","reflection"],"backgroundTag":"reflection-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}