2017-07-18 61 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