using queries generated by name with Spring Boot - spring

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

Related

MongoDB Springboot Query annotation giving only first record

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

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.

How to refer long query, written in external file, in Spring data jpa #Query

I want to write query (properties or yaml) in external file to load database. -
This is long query and does not look good to eye when placed inside #Query("long query") in XXXRepository class.
Is there way to write this query in an external file (properties, yaml, xml or json) and call that file in #Query() in spring data jpa?
You can use named queries, where the queries have to be defined in a file called META-INF/jpa-named-queries.properties. See the spring example:
User.findBySpringDataNamedQuery=select u from User u where u.lastname=?1
Reference the query by name in the annotation in your repository, here the corresponding repository example from spring:
#Query(name = "User.findBySpringDataNamedQuery", countProjection = "u.firstname")
Page<User> findByNamedQueryAndCountProjection(String firstname, Pageable page);
As suggested by Ralf Stuckert, use META-INF/jpa-named-queries.properties to store named queries
You can use \ to split the long query into multiple lines.
Example:
Customer.findNameNative=\
SELECT C.NAME \
FROM CUST_TABLE C \
WHERE CONDITIONS
I think you're finding a place to put your big query string as readable? Like Mybatis xml. As my knowledge There is no method to do that in data jpa.
But you can put that big query inside of a Stored Procedure and call it easily like below.
Call your_stored_procedure_name(param1,param2):

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