{"record":{"id":"1ce4aeab6da3163d","repo":"hibernate/hibernate-orm","slug":"invalid-attempt-to-apply-where-restriction-on-top","errorCode":null,"errorMessage":"Invalid attempt to apply where-restriction on top of custom sql-delete mapping : ","messagePattern":"Invalid attempt to apply where-restriction on top of custom sql-delete mapping : ","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/model/ast/builder/TableDeleteBuilderStandard.java","lineNumber":90,"sourceCode":"\t\tthis.whereFragment = whereFragment;\n\t}\n\n\tpublic String getSqlComment() {\n\t\treturn sqlComment;\n\t}\n\n\tpublic void setSqlComment(String sqlComment) {\n\t\tthis.sqlComment = sqlComment;\n\t}\n\n\tpublic String getWhereFragment() {\n\t\treturn whereFragment;\n\t}\n\n\t@Override\n\tpublic void setWhere(String fragment) {\n\t\tif ( mutationDetails.getCustomSql() != null && fragment != null ) {\n\t\t\tthrow new HibernateException(\n\t\t\t\t\t\"Invalid attempt to apply where-restriction on top of custom sql-delete mapping : \" +\n\t\t\t\t\t\t\tgetMutationTarget().getNavigableRole().getFullPath()\n\t\t\t);\n\t\t}\n\t}\n\n\t@Override\n\tpublic void addWhereFragment(String fragment) {\n\t\tif ( mutationDetails.getCustomSql() != null && fragment != null ) {\n\t\t\tthrow new HibernateException(\n\t\t\t\t\t\"Invalid attempt to apply where-filter on top of custom sql-delete mapping : \" +\n\t\t\t\t\t\t\tgetMutationTarget().getNavigableRole().getFullPath()\n\t\t\t);\n\t\t}\n\t}\n\n\t@Override\n\tpublic TableDelete buildMutation() {","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/model/ast/builder/TableDeleteBuilderStandard.java#L72-L108","documentation":"When an entity or collection delete is rendered from custom SQL (@SQLDelete), Hibernate must not append dynamic where fragments on top of it. TableDeleteBuilderStandard.setWhere throws HibernateException('Invalid attempt to apply where-restriction on top of custom sql-delete mapping') whenever the mutation details carry custom SQL and a non-null where fragment is applied.","triggerScenarios":"Deleting an entity mapped with @SQLDelete while something contributes a where fragment: @Where/@WhereRestrict on the entity, an enabled session @Filter applying to deletes, or other restriction logic that normally gets appended to generated deletes.","commonSituations":"Soft-delete setups combining @SQLDelete with @Where-based filtering; session filters left enabled before delete operations; filters that apply to DELETE statements colliding with custom SQL.","solutions":["Disable the contributing filter before deleting: session.disableFilter(filterName)","Move the restriction into the custom SQL: bake the predicate into the @SQLDelete statement so no external fragment is needed","Drop @Where/@WhereRestrict on that entity and apply the filtering at query level instead","Delete via a bulk JPQL or native statement that bypasses the custom-SQL delete builder"],"exampleFix":"// before\n@SQLDelete( sql = \"UPDATE Employee SET active = false WHERE id = ?\" )\n@Where( clause = \"active = true\" ) // delete routes through setWhere -> throws\n\n// after\n@SQLDelete( sql = \"UPDATE Employee SET active = false WHERE id = ? AND active = true\" )\n// no @Where on the entity; filter active rows in queries","handlingStrategy":"validation","validationCode":"// entity uses @SQLDelete: make sure no where fragment will be applied\nboolean customSqlDelete = entityType.isAffectedByCustomSql(); // or check mapping metadata\nif ( customSqlDelete ) {\n    for ( String f : enabledFilterNames( session ) ) session.disableFilter( f );\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not combine @SQLDelete with @Where/@WhereRestrict on the same entity","Bake every restriction directly into the custom delete SQL","Disable session filters before deleting entities with custom SQL deletes"],"tags":["hibernate","orm","custom-sql","soft-delete","filters","delete"],"backgroundTag":"custom-sql-where-conflict","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}