JPA查询创建顺序

我正在尝试自己学习Spring,我打算通过创建一个博客网络应用程序来做到这一点。 我已经有了基本的博客function,这是一个显示博客post的页面,以及一个提交表单的页面。 显示博客post的页面显示了最新的博客文章,以及数据库中所有博客post的标题列表。

为了从数据库中获取博客文章的有序列表,我首先在我的Repository界面中创建了一个sql查询。 这可行,但现在我想使用我可以在界面中键入方法名称而不是硬编码的sql的function。 我在这里找到了支持的关键字: http : //docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods.query-creation ,并试图实现它。

所以用我的方法findAllOrderByIdDesc()我试图实现与我的SQL查询相同的东西。 我不确定为什么它不起作用,我想我正确使用了关键字?

stackstrace(我不完全理解):
引起:org.springframework.data.mapping.PropertyReferenceException:在org.springframework.data.mapping.PropertyPath.create的org.springframework.data.mapping.PropertyPath。(PropertyPath.java:75)中找不到类型int的属性desc。 (PropertyPath.java:327)org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)位于org.springframework的org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:330)。位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)的org.springframework.data.mapping.PropertyPath.from上的data.mapping.PropertyPath.create(PropertyPath.java:353)(PropertyPath.java: 271)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)atg.springframework.data.repository.query.parser.Part。(Part.java:72)org.springframework.data。 repository.query.parser.PartTree $ OrPart。(PartTree.java:188)位于org.spri的org.springframework.data.repository.query.parser.PartTree $ Predicate.buildTree(PartTree.java:277) ngframework.data.repository.query.parser.PartTree $ Predicate。(PartTree.java:257)位于org.springframework.data的org.springframework.data.repository.query.parser.PartTree。(PartTree.java:71)。在org.springframework.data.jpa.repository的org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)的jpa.repository.query.PartTreeJpaQuery。(PartTreeJpaQuery.java:57) .query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)org.springframework.data.jpository.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)at org.springframework.data.repository.core .support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。(RepositoryFactorySupport.java:290)位于org.springframework.data.reposit的org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) Ory.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)位于org.springframework.beans.factory.support的org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)。 FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144)… 32更多

我的存储库界面:

 public interface PostRepository extends CrudRepository { @Query("select p from Post p order by p.id desc") Iterable findLastFirst(); Iterable findAllOrderByIdDesc(); } 

我的post实体:

 @Entity public class Post { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private Date date; private String title; @Lob private String body; protected Post() { date = new Date(); } public Post(String title, String body) { this.date = new Date(); this.title = title; this.body = body; } public int getId() { return id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getDate() { return date; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } 

CrudRepository有一个名为PagingAndSortingRepository的扩展

  public interface PostRepository extends PagingAndSortingRepository {} 

然后只需调用.findAll(new Sort(Sort.Direction.DESC,“id”)); 而不是findAllOrderByIdDesc();

我知道这有点晚了,但这可能对其他人有所帮助。

只需在ORDER之前输入BY ,如下所示: findAllByOrderByIdDesc

它应该工作。

我知道我比赛有点晚了但是……

另一个修复方法是更正存储库中命名查询的语法。

你有: Iterable findAllOrderByIdDesc();

它应该是: Page findAllByOrderByIdDesc();

请注意在findAll之后添加By关键字。 这标志着过滤语句的开始。

可能是我的答案很愚蠢,但是你只是尝试"from Post order by id desc"而不是"select p from Post p order by p.id desc"