{"record":{"id":"994c9503efbdc5c7","repo":"hibernate/hibernate-orm","slug":"cannot-instantiate-abstract-class-or-interface-994c95","errorCode":null,"errorMessage":"Cannot instantiate abstract class or interface","messagePattern":"Cannot instantiate abstract class or interface","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java","lineNumber":91,"sourceCode":"\t\t\t\t\t\t\tnull,\n\t\t\t\t\t\t\tnull\n\t\t\t\t\t) );\n\t\t}\n\t\treturn entity;\n\n\t}\n\n\t@Override\n\tpublic boolean isInstance(Object object) {\n\t\treturn super.isInstance( object )\n\t\t\t// this one needed only for guessEntityMode()\n\t\t\t|| proxyInterface != null && proxyInterface.isInstance( object );\n\t}\n\n\t@Override\n\tpublic Object instantiate() {\n\t\tif ( isAbstract() ) {\n\t\t\tthrow new InstantiationException( \"Cannot instantiate abstract class or interface\", getMappedPojoClass() );\n\t\t}\n\t\telse if ( constructor == null ) {\n\t\t\tthrow new InstantiationException( \"No default constructor for entity\", getMappedPojoClass() );\n\t\t}\n\t\telse {\n\t\t\ttry {\n\t\t\t\treturn applyInterception( constructor.newInstance( (Object[]) null ) );\n\t\t\t}\n\t\t\tcatch ( Exception e ) {\n\t\t\t\tthrow new InstantiationException( \"Could not instantiate entity\", getMappedPojoClass(), e );\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":73,"sourceCodeEnd":106,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java#L73-L106","documentation":"Thrown by EntityInstantiatorPojoStandard.instantiate() when Hibernate is asked to create an instance of an entity whose mapped class is abstract (or an interface). The instantiator deliberately skips constructor resolution for abstract classes (constructor = null when isAbstract()), so any runtime path that requires a concrete instance of the abstract mapped class itself - rather than one of its mapped subclasses - fails with this InstantiationException.","triggerScenarios":"Loading a row whose discriminator/subclass resolution lands on the abstract root entity (e.g., a discriminator value mapped to the abstract class or missing subclass mappings); session.refresh/instantiate directly against an abstract entity name; polymorphic references (to-one associations typed as the abstract root) where the target resolves to the root entity itself.","commonSituations":"Mapping an abstract base class with @Entity and forgetting @MappedSuperclass; adding a new discriminator value in the database without mapping the corresponding subclass; queries like em.find(AbstractBase.class, id) on hierarchies where the row belongs to an unmapped/abstract type.","solutions":["If the base type should never be instantiated, map it as @MappedSuperclass instead of @Entity","Ensure every discriminator value stored in the database has a concrete @Entity subclass mapping","Query/load concrete subclass types rather than the abstract root where instantiation is implied","If the class must stay a mapped @Entity but is abstract, add a concrete subclass covering the rows in question"],"exampleFix":"// before\n@Entity\n@Inheritance(strategy = SINGLE_TABLE)\npublic abstract class Payment { ... }   // rows exist for unknown discriminator values\n\n// after\n@MappedSuperclass            // not an entity itself\npublic abstract class PaymentBase { ... }\n\n@Entity\n@Inheritance(strategy = SINGLE_TABLE)\npublic abstract class Payment extends PaymentBase { ... } // plus concrete CardPayment/CashPayment for every discriminator value","handlingStrategy":"validation","validationCode":"// Startup guard: no mapped @Entity class should be abstract AND instantiable via root loads\nfor ( EntityType<?> t : emf.getMetamodel().getEntities() ) {\n    if ( Modifier.isAbstract( t.getJavaType().getModifiers() ) && isRootEntity(t) && !hasMappedSubclassesForAllDiscriminators(t) ) {\n        log.warn(\"Abstract mapped entity {} may receive unresolvable rows - consider @MappedSuperclass\", t.getName());\n    }\n}","typeGuard":"static boolean isConcrete(Class<?> c) { return !Modifier.isAbstract(c.getModifiers()); }","tryCatchPattern":"try {\n    return em.find(Payment.class, id);\n}\ncatch ( org.hibernate.InstantiationException e ) {\n    if ( \"Cannot instantiate abstract class or interface\".equals(e.getMessage()) ) {\n        throw new IllegalStateException(\"Row resolves to abstract root \" + e.getMessage() + \" - map missing subclass or use @MappedSuperclass\", e);\n    }\n    throw e;\n}","preventionTips":["Map never-instantiated bases as @MappedSuperclass, not @Entity","Keep discriminator values in data synchronized with mapped subclasses; add migration inserts for new types","Review each SINGLE_TABLE hierarchy after adding abstract layers or renaming subclasses"],"tags":["hibernate","jpa","entity","abstract-class","inheritance","instantiation"],"backgroundTag":"abstract-entity-instantiation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}