hibernate/hibernate-orm · error · MappingException
<any name="%s" /> mapping needs to specify 2 or more columns
Error message
<any name="%s" /> mapping needs to specify 2 or more columns
What it means
<any/> maps a polymorphic entity reference on a singular attribute and needs at least two relational values: column #1 is the discriminator (resolved through meta-type to an entity name) and the remaining column(s) are the foreign key. SingularAttributeSourceAnyImpl aborts bootstrap when fewer than two column definitions are supplied.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/SingularAttributeSourceAnyImpl.java:90
@Override
public String getSourceName() {
return jaxbAnyMapping.getName();
}
@Override
public List getColumnOrFormulaElements() {
return jaxbAnyMapping.getColumn();
}
}
);
// the list of relational values should contain 2 or more values:
// * the first represents the discriminator
// * the rest represent the fk
if ( relationalValueSources.size() < 2 ) {
throw new MappingException(
String.format(
Locale.ENGLISH,
"<any name=\"%s\" /> mapping needs to specify 2 or more columns",
jaxbAnyMapping.getName()
),
origin()
);
}
this.discriminatorSource = new AnyDiscriminatorSource() {
private final HibernateTypeSource typeSource = new HibernateTypeSourceImpl( jaxbAnyMapping.getMetaType() );
private final RelationalValueSource relationalValueSource = relationalValueSources.get( 0 );
private final Map<String,String> valueMappings = new HashMap<>();
{
for ( JaxbHbmAnyValueMappingType valueMapping : jaxbAnyMapping.getMetaValue() ) {
valueMappings.put(
valueMapping.getValue(),
sourceMappingDocument.qualifyClassName( valueMapping.getClazz() )View on GitHub (pinned to fad1729dce)
Solutions
- Map two or more columns: the first is the discriminator matching meta-type values, e.g. <column name='owner_type'/>, followed by the fk, e.g. <column name='owner_id'/>
- Add <meta-value .../> entries where discriminator values differ from entity names, and verify meta-type/id-type match the column contents
- If the schema has only one column, <any> cannot work - use a plain association or add the discriminator column first
Example fix
// before
<any name='owner' meta-type='string' id-type='long'>
<column name='owner_id'/>
</any>
// after
<any name='owner' meta-type='string' id-type='long'>
<column name='owner_type'/>
<column name='owner_id'/>
</any> Defensive patterns
Strategy: validation
Validate before calling
// Pre-flight: every <any> must declare >= 2 columns
var any = (org.w3c.dom.Element) doc.getElementsByTagName("any").item(0);
int n = any.getElementsByTagName("column").getLength();
if (n < 2)
throw new IllegalStateException("<any name='" + any.getAttribute("name")
+ "'> needs discriminator + fk columns, found " + n); Try / catch
catch (org.hibernate.boot.MappingException e) at Metadata build: the message names the <any> attribute - add the discriminator column first and the fk column(s) after it
Prevention
- When mapping interface-typed associations, always provision a discriminator column plus fk column(s)
- First column = discriminator (meta-type), remaining columns = fk - keep the order
- Prefer annotation-based @Any with explicit @Column defs where the two-column need is obvious
When it happens
Trigger: An <any name='owner' meta-type='string' id-type='long'> mapping with zero or one <column> children - e.g. only <column name='owner_id'/> - declared on an entity or component mapping.
Common situations: Modeling an interface-typed association and forgetting the discriminator column; converting a <many-to-one> to <any> without altering the table; writing hbm equivalents of @Any annotations and missing one @Column.
Related errors
- <many-to-any /> mapping [%s] needs to specify 2 or more colu
- Encountered unexpected content type [%s] for named native qu
- <many-to-any /> mapping [%s] needs to specify 2 or more colu
- Expecting just a single formula/column in context of <%s nam
- Expecting single column in context of <%s name="%s"/>, but f
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/59b44b69e6234c10.
Report an issue: GitHub.