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

  1. Remove the formula='...' attribute or remove every nested <column/>/<formula/> child - keep exactly one form
  2. 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

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


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/3ea4aab04cb60abc. Report an issue: GitHub.