JPA查询创建顺序 [英] JPA query creation order by

查看:313
本文介绍了JPA查询创建顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to learn Spring on my own, and I'm planning to do that by creating a blog web app. I already have the basic blog functionality working, which is a page to display blog posts, and a page with a form to submit one. The page to display the blog posts shows the latest blog post, and a list of the titles of all the blog posts in the db.

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

To get an ordered list of blog posts out of the database I first created an sql query inside my Repository interface. This works, but now I want to use the functionality where I can just type the method name in the interface, instead of hardcoded sql. I found the supported keywords here: http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods.query-creation , and tried to implement it.

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

So with my method findAllOrderByIdDesc() I'm trying to achieve the same thing as with my sql query. I'm not sure why it doesn't work, I think I used the keywords correctly?

stackstrace(我不完全理解):

引起:org.springframework.data.mapping.PropertyReferenceException:在org.springframework.data.mapping.PropertyPath找不到类型为int
的属性desc。(PropertyPath.java:75)
在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework .data.mapping.PropertyPath.create(PropertyPath.java:330)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping .PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)
at org.springframework.data.mapping.PropertyPath.from (PropertyPath.java:245)
在org.springframework.data.repository.query.parser.Part。(Part.java:72)
at org.springframework.data.repository.query.parser.PartTree $ OrPart。(PartTree.java:188)
at org.springframework.data.repository.query.parser.PartTree $ Predicate.buildTree(PartTree.java:277)
at org.springframework.data.repository.query.parser.PartTree $ Predicate。 (PartTree.java:257)
at org.springframework.data.repository.query.parser.PartTree。(PartTree.java:71)
at org.springframework.data.jpa.repository.query。 PartTreeJpaQuery。(PartTreeJpaQuery.java:57)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
at org.springframework.data.jpa .repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrate gy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。(RepositoryFactorySupport.java:290)
at org.springframework.data .repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
at org .springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144)
... 32更多

The stackstrace (which I don't fully understand):
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property desc found for type int at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:330) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245) at org.springframework.data.repository.query.parser.Part.(Part.java:72) at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:188) at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:277) at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:257) at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:71) at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:57) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:290) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144) ... 32 more

我的存储库界面:

public interface PostRepository extends CrudRepository<Post, Integer> {

@Query("select p from Post p order by p.id desc")
Iterable<Post> findLastFirst();

Iterable<Post> findAllOrderByIdDesc();

}

我的邮政实体:

@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

There is an extension to CrudRepository called PagingAndSortingRepository

 public interface PostRepository extends PagingAndSortingRepository<Post, Integer> {}

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

Then just call .findAll(new Sort(Sort.Direction.DESC, "id")); instead of findAllOrderByIdDesc();

这篇关于JPA查询创建顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆