我有在我的倉庫與此Hibernate過濾器:Hibernate的過濾器,不能用於FindOne CRUD操作
@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);
}
}
}
我終於有:
@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 {
它使用Spring AOP下面的類觸發以下服務:
@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;
}
}
和這些存儲庫:
@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);
}
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);
}
的問題是,當我使用groupRepository。 findById(id)過濾器已正確應用。
如果我使用CRUD核心查詢groupRepository。 findOne(id)即使在處理Aspect之後也不應用過濾器hibernateSession.enableFilter(GROUP_ACL).setParameter(「userId」,userId); 即使Java啓用過濾器,日誌文件也不會在hibernate查詢中顯示過濾器的任何跟蹤。
這個問題似乎只與.findOne。 findAll工作正常。
在Hibernate文檔中是否有某些內容表明您無法將過濾器應用於findOne方法?
嗨@Cyril你有沒有發現任何解決方案.. – Karthigeyan