jpa pagination suppress count query - spring

We are using Spring Data JPA Repository.
For pagination we are passing the Pageable object to the JPA Repository findBy Methods.
Since in our UI , we are not displaying the total count of records, we don't want the count query to be fired .
Is there any way to suppress the count query fired during pagination ?

Have your repository method return List<Entity> rather than Page<Entity>, and I believe it won't run the count query.
See "Special Parameter Handling" in Spring Data JPA documentation section "1.2.2 Defining query methods".

Related

How to return model object from jpa query

I'm developing a Spring Boot application with Spring Data JPA. I want to return the promedio field when the calificacionSemestral1 field is a specific value and the calificacionSemestral2 field is a specific value.
Entity
DER
Doing this using JPQL is it possible?
I know that I can create a DTO with the same fields or do two queries, but I want to avoid doing it if I can get the result in a single query.

Expose custom query in Spring Boot Rest API with multiple joins

I have an Spring REST Api and a MySQL Database, now I would like to expose the result of an custom query with multiple joins.
I have tried multiple suggestions that I found online but none of them were working for me so far.
What I want to do is something like a read only DTO that has all the fields of my custom query so that in the end I have one api page exposing the DTO data as JSON so my client (Angular) can read the data from there.
I already tried to:
create an #RestController with an injected EntityManager that executes a NativeQuery and then populates the DTO with the returned data but since my DTO is no Entity I get an Hibernate Mapping Exception
create a custom Repository and its Impl but with a similar outcome
place the Query inside an existing #Entity that is part of the Query statement
What am I missing here? Do I have to annotate my DTO maybe? Cuttently it's just a POJO, I think the #Entity annotation is not the right thing here since I don't want a Table created from my DTO.
Fixed it by letting the Query return an Array of type Object and afterwards mapping it to the DTO Constructor.

Pagination in Spring using #RestController, but without using Spring Data Rest Pageable?

I know using Spring Data Rest I can use the in-built functionality of Pagination Like this
Page<Product> findByCategoryId(#RequestParam("id") Long id, Pageable pageable);
However, I am in project I am using Spring mvc #RestController and want to achive the same functionality
I tried like this:-
Session currentSession = entityManager.unwrap(Session.class);
Query<Product> theQuery = currentSession.createQuery("from Product", Product.class);
theQuery.setFirstResult((pageNumber-1) * pageSize); // This is 0 based
theQuery.setMaxResults(pageSize);
List<Product> dataList = theQuery.getResultList();
return dataList;
It works but I don't get the count of total number of records in the table.
And for UI pagination I need that.
So do I have to hit 2 queries everytime first like above then 1 query to fetch record size. (Which can cause data sync problems if records are updated)
Or
Is there a better way to achieve this in SINGLE QUERY
If you need the total number of records then you must create a second query.
You could do that in one query with a subquery but then you cannot use Entities as return type.
Regarding data sync problems: If you run both queries in the same transaction there is no problem.
Btw. why do you unwrap the Hiberante Session? There is no need for that in your example.

How to limit the results of a Spring Repository with custom query

I am using a Spring repository to query a database, but because of the nature of my application I need to use a custom #Query in order to JOIN FETCH lazy collections.
This process works fine, but now I need to limit the result to a single record. I understand that Spring has the notion of findFirst or findTop1 in the method names, but this does not appear to work when you have a custom query.
How can I use a custom query and limit the result to 1 record when using a Spring repository?
you need to pass a Pageable param in your query method
#Query("select e from Entity e LEFT JOIN FETCH e.list")
public Page<Entity> find(Pageable pageable);
and call the method passing the object
repository.find(new PageRequest(0, 1));

Spring ROO sorting

I have a set of Entities generated using Spring ROO. After several commands, basically using web mvc, I have a set of links to get the contents from a database. I need to order the contents according to title, instead of the order in the table. How can I do it? Is there any Spring ROO commando or annotation to order the items.
For more information, I am using Spring Roo 1.2.1, and the finder I got is the following.
public static List<Deporte> Deporte.findAllDeportes() {
return entityManager().createQuery("SELECT o FROM Deporte o", Deporte.class).getResultList();
}
Thanks in advance.
Spring Roo uses JPA for the persistence. Search for JPA tutorials and documentation to find what you're looking for.
JPQL uses the order by clause to order results returned by a query. This order by is very similar to the SQL order by clause:
SELECT o FROM Deporte o order by o.title

Resources