Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to find all entities ordered by field in Spring Data?

I'm using Spring Data JPA and want to add a method in my base Repository interface to get all entities ordered by field order:

@NoRepositoryBean
public interface OrderedEntityDao<T extends OrderedEntity> extends EntityDao<T, Long> {
    List<T> findOrderByOrder();
}

OrderedEntity is a @MappedSuperclass entity.

But I'm getting exception when creating this bean:

Caused by: java.util.NoSuchElementException
    at java.util.ArrayList$Itr.next(ArrayList.java:854)
    at org.springframework.data.jpa.repository.query.ParameterMetadataProvider.next(ParameterMetadataProvider.java:121)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:274)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:180)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:109)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:49)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:118)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:241)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:68)

How to write this method correct?

Edit:

@MappedSuperclass
public abstract class OrderedEntity extends IdEntity implements Comparable<OrderedEntity> {
    @Nonnull
    @Column(name = "`order`")
    private Long order;
}
like image 370
Mikhail Kopylov Avatar asked Sep 24 '16 05:09

Mikhail Kopylov


People also ask

Which method is used to fetch all rows in Spring data JPA repository?

I can use the findAll() method to select * from my_table to get all columns and rows.

What is the method name to fetch all data for a entity from database in repository class?

The Iterable<T> findAll() method returns all entities that are saved to the database. The T findOne(Long id) method returns the entity whose id is given as method parameter. If no entity is found, this method returns null.

What is query method in spring data?

Query methods are methods that find information from the database and are declared on the repository interface. Spring Data has pretty versatile support for different return values that we can leverage when we are adding query methods to our Spring Data JPA repositories.

Which of the following options can be used for sorting in Spring data JPA?

In Spring Data JPA query results can be sorted in two ways: using an ORDER BY clause in a JPQL query. adding a parameter of type Sort to the query method.


2 Answers

The correct named query will be:

public interface OrderedEntityDao<T extends OrderedEntity> extends EntityDao<T> {

    public List<T> findAllByOrderBy<colname><Desc|Asc>();

}

In your case:

public List<T> findAllByOrderByOrderDesc();
public List<T> findAllByOrderByOrderAsc();
like image 105
xenteros Avatar answered Sep 28 '22 06:09

xenteros


I think this below code is work,

@NoRepositoryBean
public interface OrderedEntityDao<T extends OrderedEntity> extends EntityDao<T, Long> {
    List<T> findAllOrderByOrderAsc();
}
like image 37
Parth Solanki Avatar answered Sep 28 '22 05:09

Parth Solanki