spring data jpa multiple sorting - sorting

I am using spring data jpa and JQGrid. I need response based on multiple sort parameters.
I tried using sort parameter=column a,column b and sort order=asc but I am getting an exception
:No property column a,column b found in pojo.
It works if I would pass either of one columns as sort parameter.
Code:
Pageable pageable = JPAUtility.constructPageSpecification(pageNumber, rowsPerPage, sortColName, sortOrder);
How can I pass multiple column names in sortColName parameter?

In Spring Data you just need to add Sort parameter into findBy* method. Sort object has got a couple constructors, e.g.
Sort(Direction direction, String... properties)
which is probably exactly what you need. If you need to specify different directions for various properties then you can use
Sort(Order... orders)
where Order has property and direction: Order(Direction direction, String property)

Related

How to include the boundary values while using spring data jpa

I am writing an application which contains an entity with property named addedDate of type Date.
I have written an interface which extends MongoRepository and defined a method named
findByAddedDateBetween(Date startDate,Date endDate);
But when I query with 2 dates the output only contain list entities between the dates,Not including the entities whose addedDates is the queried dates. I want to include the boundary values also.
According to Spring JPA documentation for MongoDB, the between keyword is not inclusive in Mongo DB if you use 2 parameters.
As an alternative, you can use the spring Range class. You must use this method in order to include boundaries.
findByAddedDateBetween(Range<Date> range)
Where Range is declared like this :
Range<Date> range = Range.of(Range.Bound.inclusive(dateStart), Range.Bound.inclusive(dateEnd));

Spring Data MongoDB sorting with nullsLast as a URL query parameter

I'm using spring data MongoDB and trying to sort my results in a specific field in a specific direction and I want the null values to be the last.
The way I do that is sending the fields i want to sort in the URL as a query parameter something like the following:
/Resource?sort=date,asc
When the controller receives this request i can see the order cases in the Pageable attribute are filled correctly and the sorting is made automatically by the findAll(Pageable) method provided by spring data, the sorting is working fine, but if the date is null it will bring the Resources with a null date first, and I want the opposite.
Is there anyway to add a request parameter in the URL so the results will be ordered by date ASC and the null values at the last?
If not how can i achieve this ? maybe modifying the Pageable object but i there's no method such nullsLast()

Spring JPA findBy two or conditions

Need help with Spring JPA. Say I want to find who checkout the car when gotten a traffic ticket. So that licenseNum and
citationDate is greater than checkoutDate and citationDate is less than returnDate; or
citationDate is greater than checkoutDate and returnDate is null.
Currently,
List<SomeEntity> findByLicenseNumAndCheckOutDatetimeBeforeAndReturnDatetimeAfterOrReturnDatetimeIsNullOrderByIdDesc()
produces #1 correctly but produces all licenseNum with returnDate null.
How do I write spring JPA statement with 'or' statement correctly?
You can actually think of using a Query annotation where you can specify a custom query. Yon can choose a more meaningful method name and easy to maintain/change.

Partial match for number columns in a Spring Data JPA query

I am working with a legacy system which requires doing a query for a partial match on a number field. So I want to do something like
where ID like concat(:num, '%') where num parameter is of the type Long. Is there a way to do that using Spring Data JPA derived queries. If the id was a string then I can simply do
findByIdContaining(String id)
I would appreciate any help.
No, this is not possible with derived queries (those where the query is derived from the method name). But it's easy with an annotated #Query annotation.

Binding multiple select where option values may contain commas in Spring 3

We are having an issue with binding a multiple select element when the option values contain commas. We have tried binding to both a String and to a List<String>, but have issues with both.
When a multiple select element is posted, the value of each selected option is passed in a separate request parameter, all with the same name. For example, if the select element name is "code", the parameters might look like this:
code=ABC
code=A,B
code=XYZ
When binding to a String, Spring will automatically join these values into a comma-separated string. That is obviously an issue if one or more of the values contains a comma.
When binding to a List<String>, things work fine when multiple options are selected. In that case, Spring creates a List with an entry for each selected option. But if only one option is selected, Spring assumes the value is a comma-separated list and will split it into multiple entries.
Is there a way to tell Spring to use a different character than a comma when binding to a String? Is there a way to tell Spring not to split a single value when binding to a List<String>? Or is there another way to deal with this?
I believe this thread is related to your issue: How to prevent parameter binding from interpreting commas in Spring 3.0.5?. This Spring issue may also be helpful: https://jira.springsource.org/browse/SPR-7963
The solution provided at https://stackoverflow.com/a/5239841/1259928, which details how to create a new conversion service which uses a different string separator and wiring it into Spring config should do the trick.

Resources