我试图通过spring EntityManager
启用一个hibernate过滤器,通过tyring来切入一个用自定义注释@TenantAware
注释的服务实现方法,并添加@Around
建议该方法。我想启用自定义过滤器,在所有扩展where tenant_id = :tenantId
的实体上添加区分器BaseEntity
。因此,我创建了自定义注释,并在需要它的@Transactional
方法上使用它。它正在成功拦截该方法,但是当我记录它们时变量值显示为空,并且过滤器都没有设置。
该项目是一个spring-boot 2应用程序,我使用spring aop来创建方面。我正在使用Hibernate 5作为JPA实现提供程序。
加载时间编织SimpleJpaRepository.class
是不可能的,因为它没有暴露noarg构造函数。
这是我的TenantFilterAdvisor
课程。
package org.foo.bar.advisors;
@Aspect
@Slf4j
@Component
public class TenantFilterAdvisor {
@PersistenceContext
private EntityManager entityManager;
public TenantFilterAdvisor() {
log.debug("###########################################################################");
log.debug("###################### Tenant Advisor Filter Started ######################");
log.debug("###########################################################################");
}
@Pointcut(value = "@annotation(org.foo.bar.TenantAware)")
public void methodAnnotatedWithTenantAware() {
}
@Pointcut(value = "execution(public * * (..))")
public void allPublicMethods() {
}
@Around(value = "methodAnnotatedWithTenantAware() && allPublicMethods()")
public Object enableTenantFilter(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.debug("###########################################################################");
log.debug("###################### Before enabling tenant filter ######################");
log.debug("###########################################################################");
if (null != entityManager) {
log.debug("Tenant filter name: ", "tenantFilter");
log.debug("Tenant filter property: ", "tenantId");
log.debug("Setting tenant id to: ", new Long(10));
Session session = entityManager.unwrap(Session.class);
Filter filter = session.enableFilter("tenantFilter");
filter.setParameter("tenantId", new Long(10));
}
Object result = proceedingJoinPoint.proceed();
// Code to disable the hibernate filter goes here.
log.debug("###########################################################################");
log.debug("###################### After disabling tenant filter ######################");
log.debug("###########################################################################");
return result;
}
}
服务接口和实现类的相关部分是
public interface InventoryService {
Inventory getInventoryById(Long id);
}
@Service
public class InventoryServiceImpl implements InventoryService {
@Autowired
private InventoryRepository repo;
@Override
@Transactional
@TenantAware
public Inventory getInventoryById(Long id) {
LOG.debug("getInventoryById() called with: id = {}", id);
final Optional<Inventory> inventoryOp = repo.findById(id);
if (inventoryOp.isPresent()) {
return inventoryOp.get();
} else {
throw new InventoryNotFoundException(String.format(MESSAGE_INVENTORY_NOT_FOUND_FOR_ID, id));
}
}
}
存储库接口是
@Repository
@Transactional(readOnly = true)
public interface InventoryRepository extends BaseRepository<Inventory, Long> {
}
BaseRepository接口扩展了JpaRepository。
而方面配置类是
@Configuration
@ComponentScan(basePackages = {"org.foo.bar.advisors"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AOPConfig {
}
最后,由其他类继承的相关MappedSuperClass将过滤器定义为
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@FilterDef(
name = "tenantFilter",
parameters = @ParamDef(name = "tenantId", type = "long")
)
@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")
public abstract class BaseTransactionalEntity extends BaseEntity {
@Column(name = "tenant_id", nullable = false)
private Long tenantId;
}
如果需要详细信息,这是自定义注释类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface TenantAware {
}
我需要在会话中启用hibernate过滤器,并在继续加入点完成执行后禁用。但事实并非如此。我错过了什么?
正如Hibernate Reference Guide过滤器中所解释的,仅适用于不直接获取的实体查询。在你的代码中,你通过findById
直接获取,转换为entityManager.find
,因此是直接获取。
您可以覆盖Spring JPA存储库并重新实现findById
作为实体查询而不是直接获取,以解决此问题。