hibernate/hibernate-orm · error · MappingException

column attribute and formula attribute may not be specified

Error message

column attribute and formula attribute may not be specified together near <%s name="%s" column="%s" formula="%s" />

What it means

An hbm mapping element may describe its relational value either through the column='...' attribute or through the formula='...' attribute, but never both at once. During value-source validation Hibernate detects both attributes set on the same element and throws, interpolating the element name, its name attribute, and both attribute values so the exact spot is identifiable.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalValueSourceHelper.java:372

				errorMessage = String.format(
						Locale.ENGLISH,
						"column attribute and formula attribute may not be specified together near <%s name=\"%s\" column=\"%s\" formula=\"%s\" />",
						columnsAndFormulasSource.getSourceType().getElementName(),
						columnsAndFormulasSource.getSourceName(),
						columnsAndFormulasSource.getColumnAttribute(),
						columnsAndFormulasSource.getFormulaAttribute()
				);
			}
			else {
				errorMessage = String.format(
						Locale.ENGLISH,
						"column attribute and formula attribute may not be specified together near <%s column=\"%s\" formula=\"%s\" />",
						columnsAndFormulasSource.getSourceType().getElementName(),
						columnsAndFormulasSource.getColumnAttribute(),
						columnsAndFormulasSource.getFormulaAttribute()
				);
			}
			throw new MappingException( errorMessage, sourceDocument.getOrigin() );
		}
		//		2) and no column/formula nested elements
		if ( isNotEmpty( columnsAndFormulasSource.getColumnOrFormulaElements() ) ) {
			final String errorMessage;
			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\" />",

View on GitHub (pinned to fad1729dce)

Solutions

  1. Delete one of the two attributes - keep column='...' for a physical column or formula='...' for a derived value
  2. If both the stored column and a derived view of it are needed, map two separate properties
  3. Grep mapping files for elements carrying both column= and formula= to catch other occurrences

Example fix

// before
<property name='total' column='total_col' formula='(select sum(amount) from lines)'/>

// after
<property name='total' formula='(select sum(amount) from lines)'/>
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: no element may carry both column= and formula= attributes
var elements = doc.getElementsByTagName("*"); // walk all mapped elements
for (int i = 0; i < elements.getLength(); i++) {
    var e = (org.w3c.dom.Element) elements.item(i);
    if (!e.getAttribute("column").isEmpty() && !e.getAttribute("formula").isEmpty())
        throw new IllegalStateException("<" + e.getTagName() + " name='" + e.getAttribute("name")
                + "'> sets both column and formula");
}

Try / catch

catch (org.hibernate.boot.MappingException e) at Metadata build: the message prints element name and both attribute values - delete one of the two attributes on that element

Prevention

When it happens

Trigger: Any mapping element carrying both attributes simultaneously, e.g. <property name='total' column='total_col' formula='(select sum(x) from t)'/> or <key column='id' formula='...'/>.

Common situations: Adding a formula to an existing column-mapped element without removing the column attribute; merging attributes while copy-pasting elements; templates that pre-fill every attribute.

Related errors


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