{"record":{"id":"666c08fd2550aeb3","repo":"hibernate/hibernate-orm","slug":"invalid-attempt-to-apply-where-filter-on-top-of-cu","errorCode":null,"errorMessage":"Invalid attempt to apply where-filter on top of custom sql-delete mapping : ","messagePattern":"Invalid attempt to apply where-filter 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":100,"sourceCode":"\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() {\n\t\tif ( mutationDetails.getCustomSql() != null ) {\n\t\t\treturn new TableDeleteCustomSql(\n\t\t\t\t\tgetMutatingTable(),\n\t\t\t\t\tmutationDetails,\n\t\t\t\t\tgetMutationTarget(),\n\t\t\t\t\tsqlComment,\n\t\t\t\t\tgetKeyRestrictionBindings(),\n\t\t\t\t\tgetOptimisticLockBindings(),\n\t\t\t\t\tgetParameters()\n\t\t\t);","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/model/ast/builder/TableDeleteBuilderStandard.java#L82-L118","documentation":"Companion guard to setWhere: TableDeleteBuilderStandard.addWhereFragment appends additional where fragments to a generated delete. When the mutation is backed by custom SQL (@SQLDelete) and a non-null fragment is added, it throws HibernateException('Invalid attempt to apply where-filter on top of custom sql-delete mapping') - a custom-SQL delete cannot be extended with dynamically generated fragments.","triggerScenarios":"Deleting an entity with custom SQL delete while fragments get appended - enabled session filters contributing delete restrictions, @WhereRestrict-style dynamic restrictions, or versioned/conditional delete handling that adds fragments at build time.","commonSituations":"Soft-delete entities combined with enabled filters during delete; frameworks that auto-enable filters per request and then delete; filters added late in a session's lifecycle after entities were loaded.","solutions":["Disable the contributing filter(s) before the delete: session.disableFilter(...)","Incorporate the fragment's condition into the @SQLDelete text itself","Remove entity-level where restrictions (@Where/@WhereRestrict) that apply to deletes when custom SQL is present","Use a direct bulk delete (JPQL/native) for these entities"],"exampleFix":"// before\nsession.enableFilter( \"tenant\" );\nsession.remove( employee ); // @SQLDelete + filter fragment -> addWhereFragment throws\n\n// after\nsession.disableFilter( \"tenant\" );\nsession.remove( employee );","handlingStrategy":"validation","validationCode":"// before deleting a soft-delete entity, drop any filters that target it\nsession.getEnabledFilterNames().forEach( session::disableFilter );\nsession.remove( entity );","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Disable contributing filters before deletes on @SQLDelete-mapped entities","Include filter conditions in the custom SQL text itself","Route such deletes through bulk statements that bypass the delete builder"],"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"}