JPA: How to merge two jpa results to one pagable - spring-boot

I have two Queries:
Query1(pagable) first datasource
Query2(pagable) second datasource
I would like to do something like this
Page results = merge(query1, query2)
How i can do this in JPA ?

Are both queries on the same entity? If so you can collect the content from both in a list (List<T> content) and build a new page object. Something as follows:
Page<T> mergedResult = new PageImpl(List<T> content);
https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/PageImpl.html

Related

how to pass value dynamically to spring top function

Actually, I wanted to retrieve the Top 500 records in a table. I knew spring data has internal method findTop500 method for it. My question is can this 500 be passed dynamically? Suppose if my requirement changes to get Top1000 I don't want it to modify again.
Assuming you are asking about Spring data-methods
You can use a Pageable object to dynamically set how many entries you want to retrieve.
You can use it like this:
PageRequest pageRequest = new PageRequest(0, maxResults);
List<Record> records = repository.findAll(pageRequest);
List<Record> records = repository.findAllByKey(key, pageRequest);

How to get top results using specifications in spring data jpa?

I am quite new to spring data jpa. I am trying to use specifications while querying database. My question is using crudrepository we can method like :
findTopByUsernameOrderByUpdatedAtDesc(String username);
How can I achieve the same using specifications? I can do basic things like and or in specifications, but not very robust like we can do with criteria etc.
Specification<CustomClass> spec = Specifications.where(specification).and(username);
List<CustomClass> list = findAll(spec);
This was done as follows :
Pageable pageable = new PageRequest(0, 1, Sort.Direction.DESC, "username");
Page oneElementPage = repository.findAll(spec, pageable);
This will sort the data on username column in descending direction and return first result.
You can't express this in a Specification directly. But you can use a Pageable for this:
Page oneElementPage = repository.findAll(spec, new PageRequest(0, 1));
Gives you a Page with a single element.

Spring dynamically select #JsonView with pagination

I want to select the Json View applied to my data depending on an URL parameter. I am trying to implement this using #JsonView annotations, and I tried some solutions (1, 2). The thing is that those solutions are basen on the controller action returning a MappingJacksonValue, but I cannot use that because I am using pagination.
My action:
public ResponseEntity<Page<MyEntity>> findAll(
int viewMode,
Pageable pageable) {
result = service.findAll(pageable);
// Here I would like to apply a Json View to the result set depending
// on the variable viewMode
return new ResponseEntity<Page<MyEntity>>(
/* Resultset with the selected view applied, and paginated */,
HttpStatus.OK
);
}
To make #JsonView work with pagination, you need to set the following property to true in application.properties:
spring.jackson.mapper.DEFAULT_VIEW_INCLUSION = true
This will cause the mapper to also serialize properties which are not annotated, enabling paging to work.

How to make a custom sorting query in spring boot for a mongo db repository?

I want to put this query with #Query annotation in my repository.
This is the query:
`db.report.find({'company' : 'Random'}).sort( { 'reportDate' : -1} ).limit(1)`
Which is the best way to implement custom queries with #Query annotations or to use MongoTemplate ?
Using Mongo Template.
Criteria find = Criteria.where("company").is("Random");
Query query = new Query().addCriteria(find).with(new Sort(Sort.Direction.DESC, "reportDate"));
BasicDBObject result = mongoOperations.findOne(query, BasicDBObject.class, "collection_name");
Using Mongo Repository
Report findTopByCompanyOrderByReportDateDesc(String company)
Note that in the new version of Springboot(v2.2.5), the correct method is sort.by().
like this:
Query query = new Query().addCriteria(find).with(Sort.by(Sort.Direction.DESC, "reportDate"));

How to get count of updated records in spring data jpa?

I am using spring data jpa with hibernate as jpa persistence provider.
I am using native queries in my application. There are some update queries and I would like to get the actual number of records updated when the update query gets executed. Is there a way in spring data jpa to do this?
I am currently following the below approach;
#Modifying
#Query(value="update table x set x_provision = ?1 where x_id = ?2", nativeQuery=true)
int updateProvision(Integer provision, Integer id);
#Transactional is added on service layer.
The problem here is that when the table gets updated I get the count as 1. But there are some cases where no rows are updated. In this case also I get the count as 1. But I would like to receive the actual number of records updated which sometimes is 0.
Can someone let me know if I am doing something wrong here?
If you take a look at ModifyingExecutor, which is used to execute this query
#Override
protected Object doExecute(AbstractJpaQuery query, Object[] values) {
int result = query.createQuery(values).executeUpdate();
if (em != null) {
em.clear();
}
return result;
}
Then you see, it only delegates to native JPA infrastructure, to return number of updated elements.
So, this might have to do with used database or ORM framework configuration, other than that, the query you wrote should return correct result.

Resources