{"record":{"id":"d428dcab61543c7d","repo":"hibernate/hibernate-orm","slug":"could-not-locate-table-which-owns-column-s-refe","errorCode":null,"errorMessage":"Could not locate table which owns column [%s] referenced in order-by mapping - %s","messagePattern":"Could not locate table which owns column \\[(.+?)\\] referenced in order-by mapping - (.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java","lineNumber":983,"sourceCode":"\t\t}\n\n\t\tfor ( int i = 0, max = subclassColumnClosure.length; i < max; i++ ) {\n\t\t\tfinal String subclassColumn = subclassColumnClosure[i];\n\t\t\tfinal boolean quoted =\n\t\t\t\t\tsubclassColumn.startsWith( \"\\\"\" )\n\t\t\t\t\t&& subclassColumn.endsWith( \"\\\"\" );\n\t\t\tif ( quoted ) {\n\t\t\t\tif ( subclassColumn.equals( columnName ) ) {\n\t\t\t\t\treturn subclassColumnNaturalOrderTableNumberClosure[i];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif ( subclassColumn.equalsIgnoreCase( columnName ) ) {\n\t\t\t\t\treturn subclassColumnNaturalOrderTableNumberClosure[i];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthrow new HibernateException(\n\t\t\t\t\"Could not locate table which owns column [\" + columnName + \"] referenced in order-by mapping - \" + getEntityName()\n\t\t);\n\t}\n\n\t@Override\n\tpublic Object forceVersionIncrement(Object id, Object currentVersion, SharedSessionContractImplementor session) {\n\t\tfinal var superMappingType = getSuperMappingType();\n\t\treturn superMappingType != null\n\t\t\t\t? superMappingType.getEntityPersister().forceVersionIncrement( id, currentVersion, session )\n\t\t\t\t: super.forceVersionIncrement( id, currentVersion, session );\n\t}\n\n\t@Override\n\tpublic Object forceVersionIncrement(\n\t\t\tObject id,\n\t\t\tObject currentVersion,\n\t\t\tboolean batching,\n\t\t\tSharedSessionContractImplementor session) throws HibernateException {","sourceCodeStart":965,"sourceCodeEnd":1001,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java#L965-L1001","documentation":"When Hibernate translates an @OrderBy on an association, it resolves each referenced column to the table that owns it using the hierarchy's subclass column closure. If no table in the hierarchy owns the column, this HibernateException is thrown, naming the column and the entity. The quoted-vs-unquoted comparison in the source also means quoting style must match the mapping exactly.","triggerScenarios":"@OrderBy(\"col\") on @OneToMany/@ManyToMany/@ElementCollection where col is misspelled, is a @Formula attribute, lives only in a joined-subclass table not covered by the closure, or is quoted differently than the @Column mapping; also @OrderColumn/@OrderBy confusion (the value must be a column list, not an arbitrary expression).","commonSituations":"Ordering by a property name that maps to a formula; column renamed in a schema migration but not in the mapping; ordering by subclass-only columns in a JOINED hierarchy; quoted columns where the order-by uses the unquoted form.","solutions":["Order by a column owned by one of the hierarchy's mapped tables - verify the spelling against the target entity's @Column","If the column lives only on a subclass, move it up the hierarchy or sort the collection in memory after loading","Match the quoting exactly: if the @Column name is quoted, the @OrderBy fragment must use the same quoted form","For formula-backed attributes, order by the underlying physical column or drop the mapping-level @OrderBy"],"exampleFix":"// before: column does not exist on the target entity's tables\n@OneToMany(mappedBy = \"order\")\n@OrderBy(\"customerLastName\") // property maps to @Formula -> no owning table\nList<OrderLine> lines;\n\n// after: order by a real column\n@OrderBy(\"lastName\")","handlingStrategy":"validation","validationCode":"// fail fast: every @OrderBy column must map to a column of the target entity\nfor (Field f : entityClass.getDeclaredFields()) {\n    OrderBy ob = f.getAnnotation(OrderBy.class);\n    if (ob != null && !ob.value().isEmpty()) {\n        String col = ob.value().trim().split(\"\\\\s+\")[0].replace(\"\\\"\", \"\");\n        assert targetColumnsContain(f, col) : \"@OrderBy references unknown column \" + col;\n    }\n}","typeGuard":null,"tryCatchPattern":"try { session.createQuery(...).getResultList(); } catch (HibernateException e) { if (e.getMessage().contains(\"order-by mapping\")) { /* fix the @OrderBy column against the target mapping */ } throw e; }","preventionTips":["Review every @OrderBy against the target entity's @Column names when reviews touch associations","Never order by @Formula attributes or subclass-only columns at the mapping level","Match quoting style between @OrderBy fragments and @Column names"],"tags":["hibernate","orm","orderby","collection-mapping","column-resolution"],"backgroundTag":"invalid-orderby-column","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}