{"record":{"id":"3c3a1ed18cac15a2","repo":"hibernate/hibernate-orm","slug":"object-id-s-was-not-of-the-specified-subclass","errorCode":null,"errorMessage":"Object [id=%s] was not of the specified subclass [%s] : class of the given object did not match class of persistent copy","messagePattern":"Object \\[id=(.+?)\\] was not of the specified subclass \\[(.+?)\\] : class of the given object did not match class of persistent copy","errorType":"exception","errorClass":"WrongClassException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java","lineNumber":530,"sourceCode":"\t\t\t}\n\t\t}\n\t}\n\n\t@Nonnull\n\tprivate static Object targetEntity(\n\t\t\t@Nonnull MergeEvent event,\n\t\t\t@Nonnull Object entity,\n\t\t\t@Nonnull EntityPersister persister,\n\t\t\t@Nullable Object id,\n\t\t\t@Nonnull Object result) {\n\t\tfinal var source = event.getSession();\n\t\tfinal String entityName = persister.getEntityName();\n\t\tfinal Object target = unproxyManagedForDetachedMerging( entity, result, persister, source );\n\t\tif ( target == entity) {\n\t\t\tthrow new AssertionFailure( \"entity was not detached\" );\n\t\t}\n\t\telse if ( !source.getEntityName( target ).equals( entityName ) ) {\n\t\t\tthrow new WrongClassException(\n\t\t\t\t\t\"class of the given object did not match class of persistent copy\",\n\t\t\t\t\tevent.getRequestedId(),\n\t\t\t\t\tentityName\n\t\t\t);\n\t\t}\n\t\telse if ( isVersionChanged( entity, source, persister, target ) ) {\n\t\t\tfinal var statistics = source.getFactory().getStatistics();\n\t\t\tif ( statistics.isStatisticsEnabled() ) {\n\t\t\t\tstatistics.optimisticFailure( entityName );\n\t\t\t}\n\t\t\tthrow new StaleObjectStateException( entityName, id );\n\t\t}\n\t\telse {\n\t\t\treturn target;\n\t\t}\n\t}\n\n\t@Nullable","sourceCodeStart":512,"sourceCodeEnd":548,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java#L512-L548","documentation":"Error \"Object [id=%s] was not of the specified subclass [%s] : class of the given object did not match class of persistent copy\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when the runtime class of a passed object does not match the persistent subclass expected for its id.","commonSituations":"Typical situations: merging an object of the wrong subclass for an existing id; polymorphic associations where the id resolves to a different subclass.","solutions":["Ensure the merged object's runtime class matches the subclass of the persistent copy for the given id.","Load the persistent instance by id first and copy state onto it instead of merging a mismatched subclass instance."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}