Cyr*_*ril 5 java hibernate-filters spring-boot
我的存储库中确实有这个休眠过滤器:
@Entity
@Audited
@DiscriminatorValue(value = "GROUP")
@FilterDef(name = "groupACL", parameters = @ParamDef(name = "userId", type = "long"))
@Filters({
@Filter(name = "groupACL", condition = "app_group_id IN (SELECT DISTINCT APP_GROUP_ID FROM APP_GROUP START WITH APP_GROUP_ID IN (SELECT UG.APP_GROUP_ID FROM USER_GROUP UG JOIN APP_USER AU ON AU.APP_USER_ID = UG.APP_USER_ID WHERE USER_ID=:userId) CONNECT BY PARENT_APP_GROUP_ID = PRIOR APP_GROUP_ID)", deduceAliasInjectionPoints = false) })
public class Group extends AbstractGroup {
Run Code Online (Sandbox Code Playgroud)
它是使用 Spring AOP 和以下类触发的:
@Component
@Aspect
public class ACLFilterAspect {
private static final String GROUP_ACL = "groupACL";
@Autowired
private EntityManager em;
@Before("execution(* com.trendshift.kyn.pug.data.GroupRepository.*(..))")
public void forceFilter() {
Session hibernateSession = em.unwrap(Session.class);
....
hibernateSession.enableFilter(GROUP_ACL).setParameter("userId", userId);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我终于得到了以下服务:
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class GroupServiceImpl implements GroupService {
@Autowired
GroupRepository groupRepository;
@Override
public Group findGroupById(long id) {
Group group = groupRepository.findById(id);
return group;
}
}
Run Code Online (Sandbox Code Playgroud)
和这些存储库:
@RepositoryRestResource(exported = false)
public interface AbstractGroupRepository<T extends AbstractGroup>
extends JpaRepository<T, Long>, QueryDslPredicateExecutor<T> {
List<T> findByNameIgnoreCase(String name);
List<T> findByNameAndTypeOrderByNameAsc(String name, String type);
List<T> findByIdOrderByNameAsc(Long id);
AbstractGroup findById(Long id);
Run Code Online (Sandbox Code Playgroud)
}
public interface GroupRepository
extends AbstractGroupRepository<Group>, GroupRepositoryExtras {
List<Group> findByNameAndCustomerId(String name, Long customerId);
Iterable<Group> findByCustomerIdAndTypeIn(Long id, List<String> types);
Group findById(long id);
}
Run Code Online (Sandbox Code Playgroud)
问题是当我使用 groupRepository 时。findById(id)过滤器已正确应用。
如果我使用 CRUD 核心查询 groupRepository。findOne(id)即使在处理 Aspect 之后,也不会应用过滤器 hibernateSession.enableFilter(GROUP_ACL).setParameter("userId", userId); 即使 Java 启用了过滤器,日志文件也不会在 hibernate 查询中显示过滤器的任何痕迹。
问题似乎仅出在 .findOne 上。findAll 工作正常。
Hibernate 文档中是否有内容表明您无法将过滤器应用于 findOne 方法?
我最终监听了对 CRUDRepository 类的任何访问。不确定这是否是最好的方法,但这解决了我的问题。
@Component
@Aspect
public class ACLFilterAspect {
private static final String GROUP_ACL = "groupACL";
@Autowired
private EntityManager em;
@Before("||execution(* *(..)) && this(com.trendshift.kyn.pug.data.GroupRepository)"
+ "||execution(* *(..)) && this(org.springframework.data.repository.CrudRepository)")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3106 次 |
| 最近记录: |