2017-02-03 15 views
6

를 내가 좋아하는 것 목록 속성을 가진 클래스 exampleMatcher를 사용하는 방법을 물어. 우리는 동시에 여러 역할을 할 수있는 사용자가 있다고 가정합니다. 나는 getExampleEntity 몇 가지 속성을 가진 사용자 클래스를 보내 DB방법 목록 속성에 스프링 데이터 예를 들어, 정규 표현을 사용하는 - 쿼리 문제

에서

기관

@Entity(name = "UserEntity") 
public class User { 
    Private Long id; 
    private String name; 
    private String surname; 

    @OneToOne(cascade = CascadeType.ALL) 
    @JoinColumn 
    private Address address; 

    @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) 
    @JoinColumn 
    private List<UserRole> roles; 
} 

@Entity 
public class UserRole { 
    private Long id;  
    private String name; 
} 

을 사용자 역할을 가진 모든 사용자를 싶어. 지금 UI에서 선택한 역할 목록을 보내려고합니다. 컨트롤러

기능

@Override 
    protected User getExampleEntity() { 
     User clonedUser = new User(); 
     List<UserRole> selectedRole = new ArrayList<>(); 

//이주기 그냥 찾아위한 UI 에서 선택에 따라 DB에있는 모든 역할 추가 (롱 역할 ID : selectedUserRoleIDs를) selectedRole.add (userRoleService.find (역할 ID)); clonedUser.setRoles (selectedRole); 돌아 가기 clonedUser; } 예 Matcher를 가진 findByExample 함수를 호출 JpaRepository에서

기능.

@Override 
    public List<TObjectType> findByExample(int first, int pageSize, String sortField, Sort.Direction sortOrder, TObjectType type) 
    { 
     PageRequest pageRequest = getPageRequest(first, pageSize, sortField, sortOrder); 
     ExampleMatcher exampleMatcher = getExampleMatcher(); 
     Example<TObjectType> example = Example.of(type, exampleMatcher); 
     return repository.findAll(example, pageRequest).getContent(); 
    } 

    /** 
    * Generates an example matcher for the instance of {@link TObjectType}. 
    * This can be overriden if a custom matcher is needed! The default property matcher ignores the "id" path and ignores null values. 
    * @return 
    */ 
    protected ExampleMatcher getExampleMatcher() { 
     return ExampleMatcher.matching() 
       .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) 
       .withIgnoreNullValues(); 

    } 

속성 이름/성 또는 주소 클래스 심지어 어떤 속성을 가진 사용자를 보낼 경우이 꿈으로 작동하지만,이 목록의 역할과 작동하지 않습니다. 내가 어떻게이 문제를 해결하기 위해 어떤 조언을 주셔서 감사합니다 나는 특성으로 TObjectType의 배열 findByExample을 사용하는 방법. 고맙습니다.

편집 : 문제점을 발견했습니다. repository.findAll 기능의 코드 (org.springframework.data.repository.query.QueryByExampleExecutor # findall은)이 있습니다

생성 된 쿼리 목록 속성을 포함하지 않습니다,하지만 난 그것이의 포함되어 아무 생각이 왜

@Override 
    public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) { 

     ExampleSpecification<S> spec = new ExampleSpecification<S>(example); 
     Class<S> probeType = example.getProbeType(); 
     TypedQuery<S> query = getQuery(new ExampleSpecification<S>(example), probeType, pageable); 

     return pageable == null ? new PageImpl<S>(query.getResultList()) : readPage(query, probeType, pageable, spec); 
    } 
예제 개체. 누군가이 문제에 경험이 있습니까? 나는 약간의 설정/주석 문제 만 있다고 생각한다.

+1

나는 당신과 같은 질문을 가지고 있습니다. 이 [링크] (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example.execution) 만 SingularAttribute 속성 수 있다고하지만 나는 문서에서 볼 않았다 현재 속성 일치에 사용됩니다. 내가 SingularAttribute 속성이 있지만 그건 제외 목록을 참조 할 수있는 생각했던 연구 노력 은 ... – ejgreenwald

+0

은이 문제에 대한 해결책을 찾았나요?! 귀하의 질문에 자리하고 있으며, 나는 실제로 당신이 실제로 한 것에 관심이 있습니다. – Thodoris

답변

0

당신은 처음에는 변압기를 사용하려고합니다. 아래의 예제는 Mongodb에 대한 것이지만 접근법을 볼 수 있습니다. 내 경우 사용자 클래스는 역할 목록을 문자열로 포함합니다 :

final ExampleMatcher matcher = ExampleMatcher.matching() 
       .withIgnoreNullValues() 
       .withMatcher("roles", match -> match.transform(source -> ((BasicDBList) source).iterator().next()).caseSensitive()); 

     users = userRepository.findAll(Example.of(criteria, matcher), pageRequest);