{"record":{"id":"8a29003728d8d8f0","repo":"hibernate/hibernate-orm","slug":"cannot-perform-forcenotnull-because-internal-org","errorCode":null,"errorMessage":"Cannot perform #forceNotNull because internal org.hibernate.mapping.Column reference is null: likely a formula","messagePattern":"Cannot perform #forceNotNull because internal org\\.hibernate\\.mapping\\.Column reference is null: likely a formula","errorType":"exception","errorClass":"CannotForceNonNullableException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java","lineNumber":555,"sourceCode":"\t\t\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@Override\n\t\t\t\t\t\t\t\tpublic MetadataBuildingContext getBuildingContext() {\n\t\t\t\t\t\t\t\t\treturn AnnotatedColumn.this.getBuildingContext();\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t)\n\t\t\t);\n\t\t\tlogicalColumnName = implicitName.render( getDatabase().getDialect() );\n\t\t}\n\t\tgetBuildingContext().getMetadataCollector()\n\t\t\t\t.addColumnNameBinding( value.getTable(), logicalColumnName, getMappingColumn() );\n\t}\n\n\tpublic void forceNotNull() {\n\t\tif ( mappingColumn == null ) {\n\t\t\tthrow new CannotForceNonNullableException(\n\t\t\t\t\t\"Cannot perform #forceNotNull because internal org.hibernate.mapping.Column reference is null: \" +\n\t\t\t\t\t\t\t\"likely a formula\"\n\t\t\t);\n\t\t}\n\t\tnullable = false;\n\t\tmappingColumn.setNullable( false );\n\t}\n\n\tpublic static AnnotatedColumns buildFormulaFromAnnotation(\n\t\t\torg.hibernate.annotations.Formula formulaAnn,\n//\t\t\tComment commentAnn,\n\t\t\tNullability nullability,\n\t\t\tPropertyHolder propertyHolder,\n\t\t\tPropertyData inferredData,\n\t\t\tMap<String, Join> secondaryTables,\n\t\t\tMetadataBuildingContext context) {\n\t\treturn buildColumnOrFormulaFromAnnotation(\n\t\t\t\tnull,","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java#L537-L573","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before: formula inside a context that forces NOT NULL\n@Entity\npublic class Discount {\n    @Id                                             // id forces non-null -> formula blows up\n    @Formula(\"floor(price * 0.9)\")\n    private long discountId;\n}\n\n// after: real column for the id; formula only for derived reads\n@Entity\npublic class Discount {\n    @Id @GeneratedValue\n    private Long id;\n\n    @Formula(\"floor(price * 0.9)\")                 // read-only, no nullability forcing\n    private long discountedPrice;\n}","handlingStrategy":"validation","validationCode":"// Before boot: forbid @Formula on fields that must be non-nullable (ids etc.)\nstatic boolean formulasAreNullableOnly(Class<?> entity) {\n    for (Field f : entity.getDeclaredFields()) {\n        if (f.isAnnotationPresent(Formula.class)\n                && (f.isAnnotationPresent(Id.class) || f.isAnnotationPresent(EmbeddedId.class))) {\n            return false;\n        }\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n    sessionFactory = metadata.buildSessionFactory();\n} catch (CannotForceNonNullableException e) {\n    throw new IllegalStateException(\"A @Formula-mapped property was forced NOT NULL - map a real column\", e);\n}","preventionTips":["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"],"tags":["hibernate","formula","not-null","mapping","annotation"],"backgroundTag":"formula-not-null-conflict","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}