Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA query creation order by

Tags:

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.

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.

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?

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

My repository interface:

public interface PostRepository extends CrudRepository<Post, Integer> {

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

Iterable<Post> findAllOrderByIdDesc();

}

My Post entity:

@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;
}
like image 680
Kuurde Avatar asked Dec 04 '13 11:12

Kuurde


Video Answer


2 Answers

I know it's a bit late but this may help others.

Just put BY before ORDER, like this: findAllByOrderByIdDesc

It should work.

like image 197
Paulo Pedroso Avatar answered Sep 30 '22 19:09

Paulo Pedroso


There is an extension to CrudRepository called PagingAndSortingRepository

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

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

like image 43
Oskar Avatar answered Sep 30 '22 20:09

Oskar