hibernate/hibernate-orm · error · MappingException
Could not format discriminator value to SQL string
Error message
Could not format discriminator value to SQL string
What it means
For a polymorphic JOINED hierarchy without an explicit discriminator mapping, JoinedSubclassEntityPersister synthesizes an implicit integer discriminator from persistentClass.getSubclassId(); the surrounding try/catch converts any failure of that synthesis (DiscriminatorValue.Literal creation or Integer.toString) into MappingException('Could not format discriminator value to SQL string') during boot. Plain annotated/XML mappings should not fail here, so hitting it signals unexpected boot-model state rather than an annotation mistake.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java:201
discriminatorAlias = column.getAlias( dialect, persistentClass.getRootTable() );
}
else {
throw new MappingException( "Discriminator formulas on joined inheritance hierarchies not supported at this time" );
}
discriminatorType = DiscriminatorHelper.getDiscriminatorType( persistentClass );
discriminatorValue = DiscriminatorHelper.getDiscriminatorValue( persistentClass );
discriminatorSQLString = DiscriminatorHelper.getDiscriminatorSQLValue( persistentClass, dialect );
}
else {
explicitDiscriminatorColumnName = null;
discriminatorAlias = IMPLICIT_DISCRIMINATOR_ALIAS;
discriminatorType = basicTypeRegistry.resolve( StandardBasicTypes.INTEGER );
try {
discriminatorValue = new DiscriminatorValue.Literal( persistentClass.getSubclassId() );
discriminatorSQLString = Integer.toString( persistentClass.getSubclassId() );
}
catch ( Exception e ) {
throw new MappingException( "Could not format discriminator value to SQL string", e );
}
}
}
else {
explicitDiscriminatorColumnName = null;
discriminatorAlias = IMPLICIT_DISCRIMINATOR_ALIAS;
discriminatorType = basicTypeRegistry.resolve( StandardBasicTypes.INTEGER );
discriminatorValue = null;
discriminatorSQLString = null;
forceDiscriminator = false;
}
if ( optimisticLockStyle().isAllOrDirty() ) {
throw new MappingException( "optimistic-lock=all|dirty not supported for joined-subclass mappings [" + getEntityName() + "]" );
}
//MULTITABLES
View on GitHub (pinned to fad1729dce)
Solutions
- Declare an explicit discriminator (@DiscriminatorColumn + @DiscriminatorValue) so the implicit synthesis path is skipped
- Remove custom PersistentClass/boot-model customization for the hierarchy and retest with plain annotations
- If it reproduces on a plain mapping, minimize it and report to Hibernate (HHH JIRA)
Example fix
// before: polymorphic JOINED root, implicit discriminator, fails during boot
@Entity @Inheritance(strategy = InheritanceType.JOINED)
public abstract class Party { ... }
// after: explicit discriminator avoids implicit subclass-id synthesis
@Entity @Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "party_type")
public abstract class Party { ... } Defensive patterns
Strategy: try-catch
Try / catch
try {
SessionFactory sf = metadata.getSessionFactoryBuilder().build();
}
catch ( org.hibernate.MappingException e ) {
// implicit joined-inheritance discriminator synthesis failed
throw new IllegalStateException("SessionFactory boot failed: " + e.getMessage(), e);
} Prevention
- Prefer an explicit @DiscriminatorColumn on JOINED hierarchies to avoid the implicit subclass-id path
- Validate programmatically built PersistentClass hierarchies by booting the factory in tests
- Re-run mapping tests after Hibernate version upgrades
When it happens
Trigger: SessionFactory boot of a polymorphic JOINED hierarchy with no explicit discriminator mapping where getSubclassId() or its conversion throws — typically custom PersistentClass metadata, programmatic mapping building, or tampered inheritance configuration.
Common situations: Custom metadata sources or programmatic mappings assembling PersistentClass hierarchies; upgrading Hibernate versions where subclass-id assignment changed; partially initialized boot models.
Related errors
- Discriminator formulas on joined inheritance hierarchies not
- Identifier property '" + getPath( holder, data ) + "' cannot
- Class or package level '@NamedQuery' annotation must specify
- Class or package level '@NamedStatement' annotation must spe
- Class or package level '@NamedNativeQuery' annotation must s
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/34c9b4810a35f05e.
Report an issue: GitHub.