{"record":{"id":"1eb01fa142b4ae1c","repo":"hibernate/hibernate-orm","slug":"not-an-instance-of-id-type-idtype-getname","errorCode":null,"errorMessage":"Not an instance of id type \" + idType.getName()","messagePattern":"Not an instance of id type \" \\+ idType\\.getName\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/EntityJavaType.java","lineNumber":81,"sourceCode":"\t\tfinal var id =\n\t\t\t\toptions.getSessionFactory().getMappingMetamodel()\n\t\t\t\t\t\t.getEntityDescriptor( getJavaTypeClass() )\n\t\t\t\t\t\t.getIdentifier( value );\n\t\tif ( !type.isInstance( id ) ) {\n\t\t\tthrow new IllegalArgumentException( \"Id not an instance of type \" + type.getName() );\n\t\t}\n\t\treturn type.cast( value );\n\t}\n\n\t@Override\n\tpublic <X> T wrap(X value, WrapperOptions options) {\n\t\tfinal var entityClass = getJavaTypeClass();\n\t\tfinal var persister =\n\t\t\t\toptions.getSessionFactory().getMappingMetamodel()\n\t\t\t\t\t\t.getEntityDescriptor( entityClass );\n\t\tfinal var idType = persister.getIdentifierType().getReturnedClass();\n\t\tif ( !idType.isInstance( value ) ) {\n\t\t\tthrow new IllegalArgumentException( \"Not an instance of id type \" + idType.getName() );\n\t\t}\n\t\tfinal var entity =\n\t\t\t\toptions.getSession()\n\t\t\t\t\t\t.internalLoad( persister.getEntityName(), value, false, true );\n\t\treturn entityClass.cast( entity );\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"EntityJavaType(\" + getTypeName() + \")\";\n\t}\n}\n","sourceCodeStart":63,"sourceCodeEnd":94,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/EntityJavaType.java#L63-L94","documentation":"EntityJavaType.wrap() turns an identifier value back into an entity via internalLoad(). It first checks the supplied value is an instance of the entity's identifier class (persister.getIdentifierType().getReturnedClass()); passing an Integer where the id is Long, a Long where it is UUID, or a String for a numeric id throws IllegalArgumentException.","triggerScenarios":"Wrapping/coercing an id of the wrong Java type; hydration paths (query result assembly, cache materialization) feeding raw JDBC or JSON values into entity wrapping without normalizing numeric types; passing '5' as String for a Long id.","commonSituations":"JSON payloads delivering ids as Integer (Jackson's small-number default) into Long-id entities; switching id generation strategy or type; heterogeneous id types across an inheritance hierarchy; JavaScript front-ends sending numbers where UUIDs are expected.","solutions":["Convert the value to the exact id class first: ((Number) v).longValue(), UUID.fromString(s), etc.","Standardize id types in DTOs/APIs (configure Jackson to emit longs, not ints)","Fix the mapping so the declared id class matches what producers actually send"],"exampleFix":"// before\nObject id = mapNode.get(\"id\"); // Integer from JSON\nsession.createQuery(\"from Owner o where o.id = :id\", Owner.class)\n        .setParameter(\"id\", id); // Owner.id is Long -> Integer not an instance -> throws\n\n// after\nlong id = ((Number) mapNode.get(\"id\")).longValue();\nsession.createQuery(\"from Owner o where o.id = :id\", Owner.class)\n        .setParameter(\"id\", id);","handlingStrategy":"type-guard","validationCode":"static Object normalizeId(Class<?> idClass, Object raw) {\n    if (idClass == Long.class && raw instanceof Number n) return n.longValue();\n    if (idClass == Integer.class && raw instanceof Number n) return n.intValue();\n    if (idClass == java.util.UUID.class && raw instanceof String s) return java.util.UUID.fromString(s);\n    return raw;\n}\n// apply before passing ids into session operations or typed queries","typeGuard":"static boolean isEntityIdValue(SessionFactory sf, Class<?> entity, Object v) {\n    return sf.getMappingMetamodel().getEntityDescriptor(entity)\n        .getIdentifierType().getReturnedClass().isInstance(v);\n}","tryCatchPattern":"try {\n    Owner o = session.byId(Owner.class).load(rawId);\n} catch (IllegalArgumentException e) {\n    // wrong id runtime type: coerce to the declared id class, then retry\n    Owner o = session.byId(Owner.class).load(normalizeId(Owner.class.getDeclaredIdClass(), rawId));\n}","preventionTips":["Configure JSON deserializers to emit the declared id type (longs, not ints)","Centralize id conversion helpers instead of ad-hoc casts","Audit API DTOs: id fields must match entity @Id types exactly"],"tags":["hibernate","orm","identifier","type-mismatch","wrap"],"backgroundTag":"id-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}