2017-07-18 42 views
1

我有在我的仓库与此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方法?

+0

嗨@Cyril你有没有发现任何解决方案.. – Karthigeyan

回答

1

我最终收听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)") 
+0

你确定上面的代码中添加过滤器findOne要求,添加以下代码@Before(“执行(* org.springframework.data.repository.CrudRepository 。*(..))“) 还是行不通。你有什么其他的建议。 – Karthigeyan