Hibernate filter not working with update query

In my application I have to use one filter condition (columnABC=:inputValue) for all SELECT/INSERT/UPDATE/DELETE queries. Column ‘columnABC’ exist in all tables within application.

I have defined filter def in package-info file

@FilterDef(
        name = "columnFilter",
        parameters = @ParamDef(name = "columnABC", type = "int"),
        defaultCondition = "columnABC = :columnABC")
package com.example.entities;

import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;

Defined filter on entity

@Filter(name = "columnFilter")
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "table1")
public class Entity1 implements Serializable {
----
---

    @Column(name = "columnABC")
    private Integer ColumnABC;
}

@Component
public class Repository1 {
    @PersistenceContext
    private EntityManager entityManager;

 public void setFilter(Integer columnABC) {
        Session session = entityManager.unwrap(Session.class);
        Filter filter = session.enableFilter("columnFilter");
        filter.setParameter("columnABC", columnABC);
    }
}   

Filter is attached to session and it is getting appended for all findAll methods.For findById method it was not working so I have applied @Query and filter has attached to it.When I am running save statements filter is not getting attached to the query. What other option I can use to attach filter to update query.

@Service
public class Service1{

    @Autowired
    private Entity1Repository entity1Repository;
    
    ----
    ----

    entity1Repository.save(Entity1);
}   

 update
        table1 
    set
        column1=?,
        column2=?,
        column3=?,
        ---
    where
        column_id=?

There is no way to make this work for update/delete statements. What column does columnABC represent? Is it a tenant identifier? Or a column that is used for table partitioning?

Tenant identifier approach done thru using AbstractRoutingDataSource implementation. In our application we have 2 level data partitioning…One is achieved thru AbstractRoutingDataSource. Second level data separation trying to achieve thru filter approach.

We have two level data separation.One level achieved thru Multitenancy AbstractRoutingDataSource implementation.Second level trying to achieve using @Filter approach which has table partitioning for all select/update/insert/delete operatons

If you want to use this for table partitioning, you should update to Hibernate 6.2 which introduced the @PartitionKey annotation for exactly this purpose. Also see [Closed] Allow marking partition columns · Discussion #5353 · hibernate/hibernate-orm · GitHub

This seems to be a breaking change in hibernate 6 – hibernate 5 did apply a where clause for updates in JPA queries. Ran into this during a spring boot 2 → 3 upgrade.

For example, the following entity manager configuration:

em.unwrap(Session::class.java)
    .enableFilter(TENANT_FILTER_NAME)
    .setParameter(TENANT_FILTER_ARGUMENT_NAME, 1L)

would correctly apply a filter to a JPA query such as UPDATE Services s SET s.isDeleted = true" and append a WHERE tenantId = 1L`. This behavior is broken in hibernate 6.

Did you try using Hibernate 6.2.2.Final already? Pretty sure this was fixed already. If so, please tell the Spring folks to finally do the upgrade to 6.2.

I also faced this issue for findById, the filter is not applied for direct fetching, which breaks the @TenantId feature ([HHH-16835] - Hibernate JIRA)

See [HHH-16830] - Hibernate JIRA

@beikov Has this issue been fixed? If so, in which version was it fixed?

@Nguyen_Duc as you can see from the linked Jira, the issue has been fixed since version 6.6.0.CR1.