MongoDB Springboot Query annotation giving only first record - spring

Issue: #Query annotation giving only first result instead of Multiple expected , for Spring Boot and MongoDB.
Ideally, I am expecting 5 records based on the query , but it is giving me just 1 (the first valid response).
Here is the code in my repository:
#Query("{unixTimestamp : {$lt : ?0, $gt : ?1}}")
List<DataType> findAllByUnixTimeStampRange(long upperlimit, long lowerlimit);

Does the query work on MongoDB command-line ?
If no, then check your query, because this issue is not caused by the #Query annotation

Related

using queries generated by name with Spring Boot

I am trying to write a query in Spring Boot, I have the query written like this
, but when I search for the vehicle objects by year by doing http://localhost:8080/vehicles?year=(year) it returns to me all of the vehicle objects instead of only ones that match the year.
Try this
http://localhost:8080/vehicles?year=2001
If still error try your query in sql command, i think any mistake in your query or check again in your controller file

Distinct Column Selection using Spring JPA Specifications and Pagination gives InvalidDataAccessApiUsageException

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.

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

Resources