Distinct Column Selection using Spring JPA Specifications and Pagination gives InvalidDataAccessApiUsageException - spring

I am trying to filter results from a table based on a bunch of query parameters I receive using Spring Data JPA Specifications. I need to get results of Distinct Column which is of type UUID. All the other query params I need to query by are of type String.
So the repository method I try is findDistinctByTransactionId(Specficiation<T> spec, Pageable page)
I expect the result to be of type Page<UUID>. But I get an exception. The error message is :
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [org.springframework.data.jpa.domain.Specifications#7c900524] did not match expected type [java.util.UUID (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [org.springframework.data.jpa.domain.Specifications#7c900524] did not match expected type [java.util.UUID (n/a)]
So the problem here is the way to let the JPA know that the Distinct Column we are looking for is of type UUID.

The problem is that you are trying to combine two distinct feature of Spring Data JPA: Specifications and Query Derivation. This does not work.
What you should do is create a custom method implementation of your method which constructs a query using the JPA Criteria API and then adds the predicate you pass in as an argument.

Related

How to filter empty string from MongoDB using Spring Boot JPA query method keywords

I am trying to find all appointments that ID is not empty string and not null but can't figure out what the methods are.
Here's my repository method:
findByAppointmentIdNotNullAndAppointmentIdNot(String condition);
Internal Server Error
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'appointmentId' expression specified as 'appointmentId : Document{{$ne=}}'. Criteria already contains 'appointmentId : Document{{$ne=null}}'.
Using NotNull and Not keywords here, and pass empty string as condition, unfortunately the result still contains appointment which appointment id is empty string. Can some help?

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.

How to write a spring data jpa query method with a negated enum constant?

I'd like to query a database using a Spring Data JPA query method and retrieve the records which do not have a certain enum value. What's working is
findBySuggestionNot(Suggestion suggestion, Pageable pageable);
and then I hand in e.g. Suggestion.rejected.
What I want is
findBySuggestionNotRejected(Pageable pageable);
But this results in an error because "NotRejected" is not a property of Suggestion. Any pointers?
Using full-qualified name instand of XENUM.XXX, eg:
where a.status= com.foo.bar.Status.ACTIVED
And Be Careful, your enum should be uppercase ranther then lowercase ,camel case, otherwise the exception like "invalid path com.foo.bar.Status.ACTIVED ..." would be throw by spring. I am using Spring Boot 2.1.1.RELEASE

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