我正在尝试通过不属于 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/