{"record":{"id":"da6bd6f72d48992a","repo":"hibernate/hibernate-orm","slug":"array-element-type-error","errorCode":null,"errorMessage":"Array element type error","messagePattern":"Array element type error","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentArrayHolder.java","lineNumber":86,"sourceCode":"\t */\n\tpublic PersistentArrayHolder(SharedSessionContractImplementor session, CollectionPersister persister) {\n\t\tsuper( session );\n\t\telementClass = persister.getElementClass();\n\t}\n\n\t@Override\n\tpublic Serializable getSnapshot(CollectionPersister persister) throws HibernateException {\n//\t\tfinal int length = (array==null) ? tempList.size() : Array.getLength( array );\n\t\tfinal int length = getLength( array );\n\t\tfinal var result = (Serializable) newInstance( persister.getElementClass(), length );\n\t\tfor ( int i=0; i<length; i++ ) {\n//\t\t\tfinal Object elt = (array==null) ? tempList.get( i ) : Array.get( array, i );\n\t\t\tfinal Object elt = get( array, i );\n\t\t\ttry {\n\t\t\t\tset( result, i, persister.getElementType().deepCopy( elt, persister.getFactory() ) );\n\t\t\t}\n\t\t\tcatch (IllegalArgumentException iae) {\n\t\t\t\tthrow new HibernateException( \"Array element type error\", iae );\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\n\t@Override\n\tpublic boolean isSnapshotEmpty(Serializable snapshot) {\n\t\treturn getLength( snapshot ) == 0;\n\t}\n\n\t@Override\n\tpublic Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException {\n\t\t//noinspection unchecked\n\t\tfinal E[] sn = (E[]) snapshot;\n\t\tfinal Object[] arr = (Object[]) array;\n\t\tif ( arr.length == 0 ) {\n\t\t\treturn Arrays.asList( sn );\n\t\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentArrayHolder.java#L68-L104","documentation":"For <array>/<primitive-array> mappings, getSnapshot deep-copies every element into a fresh array of the mapped element class via persister.getElementType().deepCopy. If a copied value cannot be stored into that array (IllegalArgumentException from Array.set), Hibernate wraps it as 'Array element type error': the runtime type produced by the element type does not fit the array component type declared in the mapping.","triggerScenarios":"An <array> mapping whose element-type/element-class does not match the Java array's component type; a custom UserType whose deepCopy returns a different class than the values it receives; changing the entity field type without updating the mapping.","commonSituations":"Custom value types with sloppy deepCopy contracts; legacy hbm.xml array mappings after refactors; primitive vs wrapper type mismatches.","solutions":["Align the mapping's element-type and element-class with the actual array component type","Make custom UserType.deepCopy return the same class it received (a faithful copy)","Replace exotic array mappings with @ElementCollection on a List field where possible","Add a round-trip test that loads, snapshots, and re-saves the array collection"],"exampleFix":"<!-- before: element type mismatch -->\n<array name=\"scores\" element-class=\"java.lang.Integer\">\n    <element type=\"java.lang.Double\"/>\n</array>\n\n<!-- after: consistent element type -->\n<array name=\"scores\" element-class=\"java.lang.Double\">\n    <element type=\"java.lang.Double\"/>\n</array>","handlingStrategy":"type-guard","validationCode":"if (array != null && array.length > 0) {\n    Class<?> component = array.getClass().getComponentType();\n    Class<?> mapped = persister.getElementClass();\n    if (!mapped.isAssignableFrom(component)) {\n        throw new IllegalStateException(\"Array component \" + component.getName()\n                + \" incompatible with mapped element class \" + mapped.getName());\n    }\n}","typeGuard":"static boolean elementTypeMatches(Object[] arr, Class<?> mappedElementClass) {\n    return arr == null || arr.length == 0\n            || mappedElementClass.isAssignableFrom(arr.getClass().getComponentType());\n}","tryCatchPattern":null,"preventionTips":["Unit-test custom UserType.deepCopy: same class in, same class out","Keep mapping element-type and Java field type in sync; review mappings on refactors","Prefer List with @ElementCollection over raw array mappings for new code"],"tags":["hibernate","array-mapping","type-mismatch","usertype"],"backgroundTag":"array-element-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}