Get a range of values with Spring JPA - spring

Let's say I have an Entity class with one parameter in Spring, the parameter
private int value;
Now I have an Postgres database connected with Spring Jpa, and I have multiple entrys with the value in the database. How can I search for a certain range of the value from the database. For example I want to have the values from the range 20 till 100. I know there is are methods in Jpa but none of these really didnt help me or I used them wrong:

Related

Spring Boot CRUD findall without orderBy: is the order predictable?

Does the list returned by a CRUD repository method such as the following have a predictable order?
List<UserProfile> findAllByGroupKey (String groupKey);
The database is MySQL 5 and Spring Boot version is 2.2.7. I'd like the order of the items in the list to be the same as they were stored in the database, but I'm not sure if it's enough to omit `orderBy' to achieve this result. I couldn't find any documentation on this.
I did some testing with the specified configuration (MySQL 5 and Spring Boot 2.2.7). The answer is definitely "no", at least if the index is not a progressive number (in my case it is an UUID).
The order is in no way predictable based on how the data was entered.
The only way to get the rows in the same order as they were entered into the database is to add an additional sort variable (such as a sequence number or a timestamp) and explicitly sort the rows with "orderBy" based on that variable.

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 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.

spring data jpa multiple 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)

Resources