hibernate/hibernate-orm · error · MappingException
formula attribute may not be specified along with <column/>
Error message
formula attribute may not be specified along with <column/> or <formula/> subelement(s) near <%s name="%s" formula="%s" />
What it means
Besides the formula='...' attribute, a mapping element may declare its value with nested <column/> or <formula/> elements - one form at a time. This error fires when the formula attribute is present and the element also contains nested column/formula children; the message interpolates the element name, name attribute and the formula value.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalValueSourceHelper.java:395
if ( columnsAndFormulasSource.getSourceType().canBeNamed()
&& isNotEmpty( columnsAndFormulasSource.getSourceName() ) ) {
errorMessage = String.format(
Locale.ENGLISH,
"formula attribute may not be specified along with <column/> or <formula/> subelement(s) near <%s name=\"%s\" formula=\"%s\" />",
columnsAndFormulasSource.getSourceType().getElementName(),
columnsAndFormulasSource.getSourceName(),
columnsAndFormulasSource.getFormulaAttribute()
);
}
else {
errorMessage = String.format(
Locale.ENGLISH,
"formula attribute may not be specified along with <column/> or <formula/> subelement(s) near <%s formula=\"%s\" />",
columnsAndFormulasSource.getSourceType().getElementName(),
columnsAndFormulasSource.getFormulaAttribute()
);
}
throw new MappingException( errorMessage, sourceDocument.getOrigin() );
}
}
private static void validateUseOfColumnOrFormulaNestedElements(
MappingDocument sourceDocument,
ColumnsAndFormulasSource columnsAndFormulasSource) {
if ( isNotEmpty( columnsAndFormulasSource.getColumnAttribute() ) ) {
final String errorMessage;
if ( columnsAndFormulasSource.getSourceType().canBeNamed()
&& isNotEmpty( columnsAndFormulasSource.getSourceName() ) ) {
errorMessage = String.format(
Locale.ENGLISH,
"column attribute may not be specified along with <column/> or <formula/> subelement(s) near <%s name=\"%s\" column=\"%s\" />",
columnsAndFormulasSource.getSourceType().getElementName(),
columnsAndFormulasSource.getSourceName(),
columnsAndFormulasSource.getColumnAttribute()
);
}View on GitHub (pinned to fad1729dce)
Solutions
- Remove the formula='...' attribute or remove every nested <column/>/<formula/> child - keep exactly one form
- Prefer nested <column/> when you need column details (length, sql-type, check constraints); use the attribute only for plain single-value mappings
Example fix
// before
<property name='total' formula='(select sum(amount) from lines)'>
<column name='total_col'/>
</property>
// after
<property name='total' formula='(select sum(amount) from lines)'/> Defensive patterns
Strategy: validation
Validate before calling
// Pre-flight: formula attribute must not coexist with nested <column/>/<formula/> children
var elements = doc.getElementsByTagName("*");
for (int i = 0; i < elements.getLength(); i++) {
var e = (org.w3c.dom.Element) elements.item(i);
boolean nested = e.getElementsByTagName("column").getLength() > 0
|| e.getElementsByTagName("formula").getLength() > 0;
if (!e.getAttribute("formula").isEmpty() && nested)
throw new IllegalStateException("<" + e.getTagName() + "> mixes formula attribute with nested column/formula");
} Try / catch
catch (org.hibernate.boot.MappingException e) at Metadata build: the message names the element and the formula value - remove either the attribute or the nested children so one form remains
Prevention
- Use the attribute shorthand only for plain values; switch fully to nested elements when column details are needed
- After refactoring mappings, check that no shorthand attribute survived alongside nested children
- Run a mapping lint that flags attribute-plus-children combinations
When it happens
Trigger: An element such as <property name='x' formula='(select ...)'> that also contains a nested <column name='x_col'/> (or any <formula/>) child.
Common situations: Refactoring from nested elements to the attribute shorthand and leaving the children in place; code generation appending children to a template that already sets the attribute; merging two variants of the same mapping.
Related errors
- Expecting single column in context of <%s name="%s"/>, but f
- column attribute and formula attribute may not be specified
- column attribute may not be specified along with <column/> o
- Identifier property '" + getPath( holder, data ) + "' cannot
- Encountered unexpected content type [%s] for named native qu
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/3ea4aab04cb60abc.
Report an issue: GitHub.