使用 JPA 规范和谓词构建动态查询,findAll() 会导致 PropertyReferenceException
我有一个 API,它将接受一个日期范围。我的应用程序正在使用 Redis,我们必须通过索引日期字符串查询条目。因为 Redis 不允许像 Select * from Redis where date in (...)
这样的查询,所以我必须能够创建一个动态查询,其中包含未指定数量的“或”条件select * from Redis where date = x OR date = y OR date = z
的效果。我遵循了这些 教程 但当然它们非常模糊并且实际上并不像广告中那样工作。明天我将研究Querydsl,看看这是否是一个可行的选择。这是我的代码的要点。
@Data
@AllArgsConstructor
public class EventSpecs implements Specification<Event>{
private ArrayList<String> dates;
@Override
public Predicate toPredicate(Root<Event> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.or(
dates
.stream()
.map(value -> builder.equal(root.get("date"), value))
.toArray(Predicate[]::new)
);
}
}
存储库:
public interface EventRepository extends PagingAndSortingRepository<Event, String>, JpaSpecificationExecutor<Event> {}
以及它被调用的位置:
@Autowired
private EventRepository eventRepository;
ArrayList<String> dateRange = new ArrayList<>();
PageRequest page = PageRequest.of(0, 10);
EventSpecs specs = new EventSpecs(dateRange);
Page<Event> events = eventRepository.findAll(specs, page);
但是当我调用 findAll()
时,我总是得到 org.springframework.data.mapping.PropertyReferenceException:未找到事件类型的属性 findAll! 我知道我应该通过类属性调用,例如 findByDate()
但我不想通过静态数量的参数进行查询。我是否走在完成我需要完成的任务的正确道路上?有人有更详尽教程的资源吗? Spring 文档 是完全相同的方式,模糊,他们的代码也不起作用。
I have an API that will be taking in a date range. My application is using Redis and we have to query entries by indexed date strings. Because Redis doesn't allow a query like Select * from Redis where date in (...)
, I have to be able to create a dynamic query with an unspecified amount of 'or' conditions to the effect of select * from Redis where date = x OR date = y OR date = z
. I've followed these tutorials but of course they're extremely vague and don't actually work as advertised. I will be looking into Querydsl tomorrow to see if that's a viable option. Here's the jist of my code.
@Data
@AllArgsConstructor
public class EventSpecs implements Specification<Event>{
private ArrayList<String> dates;
@Override
public Predicate toPredicate(Root<Event> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.or(
dates
.stream()
.map(value -> builder.equal(root.get("date"), value))
.toArray(Predicate[]::new)
);
}
}
Repository:
public interface EventRepository extends PagingAndSortingRepository<Event, String>, JpaSpecificationExecutor<Event> {}
and where it's being called:
@Autowired
private EventRepository eventRepository;
ArrayList<String> dateRange = new ArrayList<>();
PageRequest page = PageRequest.of(0, 10);
EventSpecs specs = new EventSpecs(dateRange);
Page<Event> events = eventRepository.findAll(specs, page);
But when I call findAll()
I always getorg.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Event!
and I understand I should be calling by class properties like findByDate()
but I don't want to query by a static amount of parameters. Am I on the right path to accomplishing what I need to? Does anyone have resources for more thorough tutorials? Spring Documentation is the exact same way, vague and their code doesn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论