【发布时间】:2020-09-18 00:53:57
【问题描述】:
@Query("SELECT new com.ethihas.v1.blog.dto.PostDto(p.title, p.image, p.content, p.location,p.city, p.state, p.country) FROM Post p order by id desc limit 10")
List<PostDto> getAllPosts(@Param("from") Integer from, @Param("to") Integer to);
上面是我的查询,下面是 PostDto 的构造函数
public PostDto(String title, Integer image, String content, String location, String city, String state, String country) {
this.title = title;
this.image = image;
this.content = content;
this.location = location;
this.city = city;
this.state = state;
this.country = country;
}
一切看起来都很好,但它导致以下异常
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: limit near line 1, column 167 [SELECT new com.ethihas.v1.blog.dto.PostDto(p.title, p.image, p.content, p.location,p.city, p.state, p.country) FROM com.ethihas.v1.blog.model.Post p order by id desc limit 10]
【问题讨论】:
-
it looks like JPQL doesn't support
LIMIT。您可以使用Pageable作为解决方法。 -
尝试使用 PageRequest PageRequest(int,int,org.springframework.data.domain.Sort) 在 org.springframework.data.domain.PageRequest 中具有受保护的访问权限
-
不是确切的解决方案,但可以提供帮助,请检查 - stackoverflow.com/questions/3479128/…
-
也是 Pageable 的解决方案 - stackoverflow.com/a/36790914/7505731
标签: mysql spring-boot jpa spring-data-jpa