java - Spring数据查询示例

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

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

我当前采用的方法是按示例查询。 platform 字段的过滤工作正常,但 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));
}

最佳答案

TL;DR

Spring Data JPA 中的“按示例查询”不支持集合值属性

长版

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

Only supports starts/contains/ends/regex matching for strings and exact matching for other property types

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

在同一份文件中,它进一步写道:

Only SingularAttribute properties can currently be used for property matching.

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

关于java - Spring数据查询示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44380760/

相关文章:

java - 无法确定 Hibernate PersistenceProvider

java - 为什么 Spring DependsOn 注释不能用于自动连接接口(interface)

java - Spring @RequestParam 和 req.getParameter ("xx") 的工作方式不同吗?

java - MongoDB中计算的分组字段

java - 如何在没有@Id的情况下使用spring Repository?

java - 使用文件输入/输出流方法安全存储数据

java - 如何在 JasperReport 中将 svg 字节数组显示为图像?

java - J2ME 支持 Apache Lucene 吗?

java - 如何在 Java 中为 javascript 小部件记录印象(和数据)?

java - 复合 ID JPA2 和 Spring Data 不起作用