{"record":{"id":"0b31a576bd026db3","repo":"hibernate/hibernate-orm","slug":"entity-name-referenced-by-many-to-one-required-s","errorCode":null,"errorMessage":"entity name referenced by many-to-one required [%s]","messagePattern":"entity name referenced by many-to-one required \\[(.+?)\\]","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java","lineNumber":3838,"sourceCode":"\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static class ManyToOneFkSecondPass implements FkSecondPass {\n\t\tprivate final MappingDocument mappingDocument;\n\t\tprivate final ManyToOne manyToOneBinding;\n\n\t\tprivate final String referencedEntityName;\n\t\tprivate final String referencedEntityAttributeName;\n\n\t\tprivate ManyToOneFkSecondPass(\n\t\t\t\tMappingDocument mappingDocument,\n\t\t\t\tSingularAttributeSourceManyToOne manyToOneSource,\n\t\t\t\tManyToOne manyToOneBinding,\n\t\t\t\tString referencedEntityName) {\n\t\t\tif ( referencedEntityName == null ) {\n\t\t\t\tthrow new MappingException(\n\t\t\t\t\t\t\"entity name referenced by many-to-one required [%s]\"\n\t\t\t\t\t\t\t\t.formatted( manyToOneSource.getAttributeRole().getFullPath() ),\n\t\t\t\t\t\tmappingDocument.getOrigin()\n\t\t\t\t);\n\t\t\t}\n\t\t\tthis.mappingDocument = mappingDocument;\n\t\t\tthis.manyToOneBinding = manyToOneBinding;\n\t\t\tthis.referencedEntityName = referencedEntityName;\n\t\t\tthis.referencedEntityAttributeName = manyToOneSource.getReferencedEntityAttributeName();\n\t\t}\n\n\t\t@Override\n\t\tpublic Value getValue() {\n\t\t\treturn manyToOneBinding;\n\t\t}\n\n\t\t@Override\n\t\tpublic String getReferencedEntityName() {","sourceCodeStart":3820,"sourceCodeEnd":3856,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java#L3820-L3856","documentation":"ManyToOneFkSecondPass creates the foreign key for a bound many-to-one and requires the referenced entity name; its constructor guards against null because FK creation has nothing to point at. In practice a null name only slips through unusual programmatic source construction, since normal hbm.xml parsing demands class= or entity-name= on <many-to-one> before this point.","triggerScenarios":"A hand-implemented SingularAttributeSourceManyToOne (custom metadata source or tooling) whose referenced-entity accessor returns null; a corrupted/hand-mangled hbm.xml where the parser produced no class or entity-name for a many-to-one.","commonSituations":"Custom Binding or metadata-source tooling building attribute sources programmatically; edge-case mappings generated by external tools.","solutions":["Ensure the <many-to-one> element defines class= or entity-name=","If building attribute sources programmatically, never return null from the referenced entity name accessor - validate before registering the source","Regenerate or repair the malformed mapping file if a tool produced it"],"exampleFix":"// before\n<many-to-one name='dept' column='dept_id'/>\n\n// after\n<many-to-one name='dept' class='com.acme.Department' column='dept_id'/>","handlingStrategy":"validation","validationCode":"NodeList mto = doc.getElementsByTagName(\"many-to-one\");\nfor (int i = 0; i < mto.getLength(); i++) {\n    Element m = (Element) mto.item(i);\n    boolean hasTarget = m.getAttributeNode(\"class\") != null || m.getAttributeNode(\"entity-name\") != null;\n    if (!hasTarget) {\n        throw new IllegalStateException(\"<many-to-one '\" + m.getAttribute(\"name\") + \"'> has no class/entity-name\");\n    }\n}","typeGuard":null,"tryCatchPattern":"catch (MappingException e) at bootstrap; add class= or entity-name= to the named many-to-one. If sources are built programmatically, fix the accessor that returned null.","preventionTips":["Always declare class= or entity-name= on <many-to-one>","In programmatic metadata sources, validate required fields before registering them","XSD validation catches this shape before runtime"],"tags":["hibernate","hbm-mapping","many-to-one","foreign-key","missing-target-entity"],"backgroundTag":"missing-target-entity","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}