hibernate/hibernate-orm · error · CannotForceNonNullableException
Cannot perform #forceNotNull because internal org.hibernate.
Error message
Cannot perform #forceNotNull because internal org.hibernate.mapping.Column reference is null: likely a formula
What it means
AnnotatedColumn.forceNotNull() flips both the AnnotatedColumn and its wrapped org.hibernate.mapping.Column to NOT NULL. Columns built from @Formula have no underlying mapping Column (mappingColumn == null), so forcing not-null on a formula-mapped property throws CannotForceNonNullableException with the hint that the property is 'likely a formula'.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java:555
return false;
}
@Override
public MetadataBuildingContext getBuildingContext() {
return AnnotatedColumn.this.getBuildingContext();
}
}
)
);
logicalColumnName = implicitName.render( getDatabase().getDialect() );
}
getBuildingContext().getMetadataCollector()
.addColumnNameBinding( value.getTable(), logicalColumnName, getMappingColumn() );
}
public void forceNotNull() {
if ( mappingColumn == null ) {
throw new CannotForceNonNullableException(
"Cannot perform #forceNotNull because internal org.hibernate.mapping.Column reference is null: " +
"likely a formula"
);
}
nullable = false;
mappingColumn.setNullable( false );
}
public static AnnotatedColumns buildFormulaFromAnnotation(
org.hibernate.annotations.Formula formulaAnn,
// Comment commentAnn,
Nullability nullability,
PropertyHolder propertyHolder,
PropertyData inferredData,
Map<String, Join> secondaryTables,
MetadataBuildingContext context) {
return buildColumnOrFormulaFromAnnotation(
null,View on GitHub (pinned to fad1729dce)
Solutions
- Stop forcing the property non-nullable: remove the not-null requirement (drop it from ids / make the owning mapping optional)
- Map a real column instead of a formula if the value must be NOT NULL in DDL
- Keep @Formula for read-only derived values only and never include it in identifiers
Example fix
// before: formula inside a context that forces NOT NULL
@Entity
public class Discount {
@Id // id forces non-null -> formula blows up
@Formula("floor(price * 0.9)")
private long discountId;
}
// after: real column for the id; formula only for derived reads
@Entity
public class Discount {
@Id @GeneratedValue
private Long id;
@Formula("floor(price * 0.9)") // read-only, no nullability forcing
private long discountedPrice;
} Defensive patterns
Strategy: validation
Validate before calling
// Before boot: forbid @Formula on fields that must be non-nullable (ids etc.)
static boolean formulasAreNullableOnly(Class<?> entity) {
for (Field f : entity.getDeclaredFields()) {
if (f.isAnnotationPresent(Formula.class)
&& (f.isAnnotationPresent(Id.class) || f.isAnnotationPresent(EmbeddedId.class))) {
return false;
}
}
return true;
} Try / catch
try {
sessionFactory = metadata.buildSessionFactory();
} catch (CannotForceNonNullableException e) {
throw new IllegalStateException("A @Formula-mapped property was forced NOT NULL - map a real column", e);
} Prevention
- Reserve @Formula for derived, read-only, nullable properties
- Never use @Formula inside identifiers or required columns
- When DDL must be NOT NULL, use a real column with a database default or trigger
When it happens
Trigger: A property mapped with @Formula is pulled into a context that forces non-nullability - e.g. it is (part of) an identifier, a required association target column set, or another mapping path that calls forceNotNull() while the column set contains a formula-derived AnnotatedColumn.
Common situations: Using @Formula to synthesize an id or part of a composite key; putting optional=false-style requirements on formula-backed properties; combining @Formula with embedded ids during refactors.
Related errors
- '@AttributeAccessor' annotation must specify a 'strategy'
- Write expression in '@ColumnTransformer' for property '${pro
- '@ColumnDefault' may only be applied to single-column mappin
- '@GeneratedColumn' may only be applied to single-column mapp
- '@Check' may only be applied to single-column mappings but '
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/8a29003728d8d8f0.
Report an issue: GitHub.