Spring Boot Hibernate过滤器不适用于在merge方法内执行的select和update语句

bq9c1y66  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(97)

我有一个JPA实体,其中的过滤器定义如下:

@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")
public abstract class TenantAwareBaseEntity extends BaseEntity {
    @Column(name = "tenantId", nullable = false)
    private String tenantId;
}

在像这样调用merge方法之前,我使用以下代码启用过滤器:

entityManager
             .unwrap(Session.class)
             .enableFilter("tenantFilter")
             .setParameter("tenantId", TenantContext.getCurrentTenantId());

当merge方法执行时,它执行两个SQL语句,一个select和一个update,但是当我检查为这些select和update语句生成的SQL语句时,我没有看到任何tenantId的过滤器,如下所示:

select.....where tenatId = ?

or

update.....where tenantId = ?

这个过滤器可以在其他方法上工作,但是不能在merge方法上工作。有什么方法可以在merge方法上启用休眠过滤器吗?

5hcedyr0

5hcedyr01#

不,你不能在merge中使用它,如果你的代码正确,当你使用过滤器进行查询时,你不应该在第一时间访问实体,所以Hibernate没有理由在其他地方使用过滤器。

相关问题