findAll() and Between two dates with query by Example<S> JPA? - java

Im doing this right now on my JPARepository and it I love how it works and how easy it is to replicate with other Entitys
<S extends MyObject> Page<S> findAll(Example<S> example, Pageable pageRequest);
Now I want the same format with Example but being able to find Between 2 dates, is it possible without an #Query?
<S extends MyObject> Page<S> findByDateBetween(Date startDate, Date endDate, Example<S> example, Pageable pageRequest);
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example
EDIT:
After some research
I think the only way is doing a custom query, i dont like that approach because the Example<S> example class has a lot of propperties.

Yes, spring JPA has the method findAllByPublicationTimeBetween() which retrieves articles published between two given hours.
Refer: [https://www.baeldung.com/spring-data-jpa-query-by-date]

Related

Not Equals Query on Spring Boot REST/JPA Service

I'm brand new to Spring Boot, and I've created a very basic REST service that uses JPA, and exposes the RepositoryRestResource for CRUD and query operations on my model:
#RepositoryRestResource
public interface CatalogueOrderRepository extends JpaRepository<CatalogueOrder, Long>,
QuerydslPredicateExecutor<CatalogueOrder> {
}
Using this, I'm able to perform queries that involve searching for values, pagination, and ordering, for instance:
?page=0&size=5&sort=priority,desc&orderStatus=submitted
Is it possible to search for values that are not equal, without any additional work? For instance, all orders where the orderStatus is NOT equal to 'submitted'.
I notice that the Predicate interface has a not() method, though I'm not sure if it's related.
For such cases you should do some work. There are different approaches to do that. See Spring docs and examples about JPA.
E.g. you can use #Query or specifications.
You can try "Query creation from method names".
Let's say you want to search Orders by orderstatus <> submitted,
List<Order> findByOrderstatusNot(String orderstatus);

Which is faster custom query using #Query vs findAll() thru CrudRepository?

I was writing Java code to get all the rows from the database table.
I was using CrudRepository and used this method below.
public interface StudentRepository extends CrudRepository<Student, Long>
{
public List<Student> findById(long id);
}
or
#Query(value = "SELECT s FROM Student s")
List<Student> customMethod(long id);
Which method is faster? Does Java internal method provide faster than our custom query?
Thanks in advance.
The default findById provided by Spring Data Repository and a query-annotated method have significantly different semantics. But, to keep it short, I will try to focus on differences in performance exclusively.
Unless you have query cache enabled, a query-annotated method will always hit the database with a query.
findById, on the other hand, ultimately calls EntityManager.find(). EntityManager.find() looks up the entity in the persistence context first. That means if the entity has already been loaded into the context, the call will not hit the underlying database.
As a side note, if you're curious as to how Spring implements the default repository methods, have a look at the source of SimpleJpaRepository.
You have to understand that findAll() method eventually generates the query for the selection. The only way to prove that is to test it. I don't think you will gain a significant performance boost. JPA's, on another hand, query generation is extremely easy to understand and use. So, if you hesitate between using one or the other, I would stick to findAll() JPA or spring data repository methods.

#query with only sort by . Without limit

I`m trying to do select with #Query annotation with only sort by.
I searched a lot but without any luck. Currently I implemented it to work but with Pageable class that want me to set limit . Is there any other way to accomplish that without limit? Only normal select with order by
#Query("SELECT new com.concretepage.entity.Employee(employeeId,firstName,lastName,salary)FROM Employee")
List<Employee> getBySalaryOrdered(Pageable pageable);
I`m searching for way to do it without Pageable. I want only to have sort without any limits
Instead of using #Query annotation you could use query generation based on a method definition inside the interface that extends CrudRepository or JpaRepository.
In your case, it would be:
List<Employee> findAllByOrderBySalaryAsc();
Check docs for more.

Spring Data Rest search by keyword

I'm trying to find the solution for implementing search by keyword. Final API should look like /persons/search?keyword=test. The search should check several columns of persons table (firstName, lastName, school, ...).
In the result list, I need to have the list of persons which contains keyword at least in one column.
What will be the best solution to implement that by using Spring Data Rest?
Using #Query annotation on a repository, you can do something like this:
public interface PersonRepository extends CrudRepository<Persons, Long> {
#Query("SELECT p FROM Persons where LOWER(p.firstname) like :key%"
+ " or LOWER(p.lastname) like :key%" )
public List<Person> searchBy(#Param("word") String key);
}
NOTE: I did a similar thing, but I do not have the exact code right now, so just check out the syntax, but what I was able to achieve with this was the ability to search on multiple columns with a single key.

Spring-Data JPA CrudRepository returns Iterable, is it OK to cast this to List?

I'm writing a code-gen tool to generate backend wiring code for Spring-boot applications using Spring-Data-Jpa and it's mildly annoying me that the methods in the CrudRepository return Iterable rather than List, as iterable doesn't provide quite enough functionality, but List does, so I'm looking for the best way to convert the iterable into a list.
I saw this post on changing an iterable to a collection and I was wondering, rather than using a library like Guava or implementing my own function to do the conversion, why not just cast it to List? Is there something wrong with doing that that I don't know about?
Edit:
I ask because since it's a code-gen tool it's not reasonable to make it generate code that introduces dependencies on 3rd party libraries, and writing my own function to do the conversion also isn't really reasonable because it would have to live somewhere and I'd rather not have that in the generated code. A simple cast will work, if a little ugly, but just wondered if there's something I'm missing?
You mentioned [spring-data-jpa] so i guess you use JPA. In this case use JpaRepository instead of CrudRepository where the methods return List's like you want it.
No, I don't think it's OK.
While a List is guaranteed to be an Iterable an Iterable may not be a List. This means that if you do cast an Iterable to a List it may fail at runtime. Even if it works, there's no guarantee that it will continue to work in the future as it could change in new versions of Spring Data JPA without breaking the interface's contract.
Instead of using a cast, you should declare your own query methods that return List.
Alternatively you may use Streamable.of(iterable).toList() for doing the conversion. This answer also contains some background why Iterable was chosen as the return type for these methods.
Your interface can still extends the CrudRepository, you can just add a new method findAll returning a list. Like example below:
#Repository
public interface DataRepository extends CrudRepository<Data, Long> {
#Override
List<Data> findAll();
}
If you have an "abstract" repository to be extended by all your repositories, you can add this method too, so it will has effect to all your repositories. Like example below:
#NoRepositoryBean
public interface GenericRepository<T> extends CrudRepository<T, Long> {
#Override
List<T> findAll();
}
Starting from Spring Data 3 there is another option: ListCrudRepository.
This blog post covers why CrudRepository chose Iterable instead of List.
https://spring.io/blog/2022/02/22/announcing-listcrudrepository-friends-for-spring-data-3-0
Hi I know I am late to the party here but if you want to convert an Iterable to a Collection you can do it this way.
private <T> Collection<T> map(final Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toCollection(HashSet::new));
}
After that, du can get a List by .stream().toList(); on the Collection. Hope it helps
Simply, you can make your repository to extend JpaRepository not CrudRepository. This will work perfectly. Here findAll returns a List not an Iterable.

Categories