如何使用spring数据示例匹配器列表属性 - 查询问题

sme*_*dak 9 java arraylist findby spring-data-jpa

我想问一下如何使用带有List属性的class的exampleMatcher.让我们假设,我们有一个可以同时拥有多个角色的用户.我希望从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;
}
Run Code Online (Sandbox Code Playgroud)

我发送一个带有一些属性的User类到getExampleEntity.我正在尝试从UI发送所选角色的列表.

控制器中的功能

@Override
    protected User getExampleEntity() {
        User clonedUser = new User();
        List<UserRole> selectedRole = new ArrayList<>();
        // this cycle just find and add all roles in db based on selection from UI
        for (Long roleID : selectedUserRoleIDs)
           selectedRole.add(userRoleService.find(roleID));
        clonedUser.setRoles(selectedRole);
        return clonedUser;
    }
Run Code Online (Sandbox Code Playgroud)

来自JpaRepository的函数,它使用示例Matcher调用findByExample函数.

    @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();

    }
Run Code Online (Sandbox Code Playgroud)

如果将发送具有属性名称/姓氏的用户甚至是Address类中的任何属性,它将作为一个梦想,但它不适用于List角色.我将非常感谢如何解决这个问题以及如何使用带有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);
    }
Run Code Online (Sandbox Code Playgroud)

生成的查询不包含列表属性,但我不知道为什么它包含在Example对象中.有人遇到过这个问题吗?我想只有一些设置/注释问题.

Igo*_*hin 5

您应该首先尝试使用变压器。下面的示例是针对 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);
Run Code Online (Sandbox Code Playgroud)

  • 为了避免“不可转换类型”错误,请使用 `match -&gt; match.transform(source -&gt;Optional.of(((BasicDBList) source.get()).iterator().next())).exact()` (4认同)