Why is a Hibernate filter not active when using Spring Data JPA Repositories

1.1k Views Asked by At

We try to use Hibernate Filters together with Spring Data Repositories. This works fine for all methods defined in our Interface, but it does not work for the default method findOne() defined on org.springframework.data.repository.CrudRepository<User, Long> directly.

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
    User getById(Long id);
}

@Transactional
public User getUserById(Long userId) {
    Filter filter = entityManager.unwrap(Session.class).enableFilter("TENANT_FILTER");
    filter.setParameter("tenantId", 1L);
    // return userRepository.findOne(userId); -> NOK, Filter NOT active
    return userRepository.getById(userId); -> OK, Filter active
}

What is the reason the TENANT_FILTER is not active for findOne(..)?

0

There are 0 best solutions below