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.
Related
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));
How I can handle a field in PostgreSQL whit datatype 'jsonb'.
I want to map it to an entity and use it whit a JPQL Query (check if a field exists with the given ID)
It should be performant as possible.
I have a table where the date field is a varchar. Since it is a legacy application I do not want to change the datatype. I want to write the following query in criteria. Any help will be highly appreciated
select * from ucms_vu vu where vu.case_e_dt is not null and to_date(vu.case_e_dt,'YYYY-MM-DD') = to_date('2018-02-02','YYYY-MM-DD')
Using purely JPA, I believe that is not possible. Because you are wanting to do a specific manipulation in a not manipulable data.
If you use Hibernate too, #Formula annotation resolves your problem.
You can create a attribute in your ucms_vu entity:
#Formula("to_date(case_e_dt,'YYYY-MM-DD')")
private Date caseE;
And your criteria would use this attribute to perform the query.
For Example:
Criteria criteria = session.createCriteria(Ucms_vu.class, "ucms");
criteria.add(Restrictions.eq("ucms.caseE", new Date("your date 02/02/2018"));
criteria.list();
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.
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