Spring数据按示例查询

时间:2017-06-06 03:06:08

标签: java spring spring-data spring-data-jpa

我正在尝试过滤掉不属于AppService loginallowedusers实体的用户的当前App我的platform返回的结果。

我采用的当前方法是Query-By-Example。 allowedusers字段的过滤效果很好但@ManyToMany @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @JoinTable(name = "app_alloweduser", joinColumns = @JoinColumn(name="apps_id", referencedColumnName="id"), inverseJoinColumns = @JoinColumn(name="allowedusers_id", referencedColumnName="id")) private Set<User> allowedusers = new HashSet<>(); public void setAllowedusers(Set<User> users) { this.allowedusers = users; } 过滤器似乎没有效果?

应用实体

@Override
@Transactional(readOnly = true)
public Page<AppDTO> findAll(RequestParams requestParams, Pageable pageable) {
    App newApp = new App();
    Example<App> appExample;

    // set to current logged in user
    User user = new User();
    user.setLogin(SecurityUtils.getCurrentUserLogin());
    Set<User> users = new HashSet<User>();
    users.add(user);
    newApp.setAllowedusers(users);

    // set platform selected
    if (requestParams.platform != null) {
        newApp.setPlatform(requestParams.platform);
    }

    appExample = Example.of(newApp);

    // find by example
    Page<App> results = appRepository.findAll(appExample, pageable);
    return results.map(app -> appMapper.toDto(app));
}

AppService服务

<TargetConnectionString>Data Source=XXX;User ID=XXX;Password=XXX;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True</TargetConnectionString>

1 个答案:

答案 0 :(得分:0)

TL; DR

Spring Data JPA中的Query By Example不支持集合值属性

长版

The documentation describes the following limitations of Query By Example强调我的:

  

仅支持字符串的开始/包含/结束/正则表达式匹配以及其他属性类型的完全匹配

因此,期望应用IN逻辑肯定超出了当前实现的范围。

然后在同一份文件中再说:

  

目前,只有SingularAttribute属性可用于属性匹配。

这是一种令人困惑的陈述方式:仅适用于简单属性