Hibernate omits tenantId clause on some queries

13 Views Asked by At

I'm using Hibernate v6.4.1.Final and Postgres 15 with the @TenantId annotation on my Hibernate Entities.

The Hibernate docs say this means that entities will always be filtered based on the current tenantId from CurrentTenantIdentifierResolver. I thought this was working well for me, but I recently noticed that for some entities, the where tenant_id = ? clause is not included!

The queries missing the WHERE tenant_id = ? clause use the same repository method: SimpleJpaRepository.findById. Their tenant_id column is setup in exactly the same way:

  @Column(name = "tenant_id", nullable = false, updatable = false)
  private Long tenantId;

For example the query from CExpenseRepository.findById:

public interface CExpenseRepository
    extends JpaRepository<CExpenseBE, Long>,
        PagingAndSortingRepository<CExpenseBE, Long> {}

INFO [tenantId:1] --- n.t.d.l.l.SLF4JQueryLoggingListener Query:["select ceb1_0.id,ceb1_0.amount,set1_0.id,set1_0,set1_0.tenant_id,ceb1_0.tenant_id from c_expense ceb1_0 join s_expense_type set1_0 on set1_0.id=ceb1_0.s_expense_type_id where ceb1_0.id=?"], Params:[(4)]

A different type of entity that is set up exactly the same does produce the tenant_id clause:

INFO [tenantId:1] --- n.t.d.l.l.SLF4JQueryLoggingListener ]Query:["select sd1_0.id,sd1_0.address1 from sales_order sob1_0 join shipping_destination sd1_0 on sd1_0.id=sob1_0.shipping_destination_id where sob1_0.tenant_id = ? and sob1_0.id=?"], Params:[(1,2)]

0

There are 0 best solutions below