Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

How to return a page of results from a typed query ?

$
0
0
Hello,

I have a custom repository with a TypedQuery that is supposed to return a Page of results and not a List of results.

But I can't see how I can just extract a page from the results.

Here is the method implementation:
Code:

    @Override
    @Transactional(readOnly = true)
    public Page<ElearningSubscription> searchWithDistinctUserAccounts(String searchTerm, Pageable page) {
        String sqlStatement = "SELECT es FROM ElearningSubscription es, UserAccount ua WHERE es.userAccount.id = ua.id AND (LOWER(ua.firstname) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(ua.lastname) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR LOWER(ua.email) LIKE LOWER(CONCAT('%', :searchTerm, '%')) OR ua.id = :searchTerm";
        if (searchTerm.contains(" ")) {
            String[] bits = searchTerm.split(" ");
            String firstname = bits[0];
            String lastname = bits[1];
            sqlStatement += " OR (LOWER(ua.firstname) LIKE LOWER(CONCAT('%', " + firstname + ", '%')) AND LOWER(ua.lastname) LIKE LOWER(CONCAT('%', " + lastname + ", '%')))";
        }
        sqlStatement += " GROUP BY ua.id ORDER BY ua.firstname, ua.lastname, es.subscriptionDate DESC";
       
        TypedQuery<ElearningSubscription> query = elearningSubscriptionRepository.getEntityManager().createQuery(sqlStatement, ElearningSubscription.class);

        query.setParameter("searchTerm", searchTerm);
       
//        Page<ElearningSubscription> elearningSubscriptions = query.getResultList();
       
//        return elearningSubscriptions;
        return null;
    }

I guess I should be able to make use of the passed in page method argument somewhere in the query creation.

Any clue ?

Kind Regards,

Viewing all articles
Browse latest Browse all 297

Trending Articles