Not Equals Query on Spring Boot REST/JPA Service - java

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);

Related

How does Spring JPA derive queries?

I am wondering how does Spring JPA derive queries from methods. As an example, if I was to type
interface CarRepo extends CrudRepository<Car, Long>{ findByCarMake(Make make) }
my query would be automatically derived from the method and would be something as "SELECT * from Car WHERE carMake = xxxxx"
I do understand this concepts but I would like to understand how it works behind the scenes. So, how does it actually derive a query from the method name?
I am aiming at creating a similar thing to suit our needs for a NestJs project so in Typescript not Java and also for an..."unorthodox" database which does not have such support out of the box( Neo4J).
I ll be very grateful to whom can and will help me.
Spring Data JPA derives the queries from the method names in your repository.
There are certain keywords that are reserved by Spring. One the one hand, there are query subject keywords like findBy, existsBy, countBy, etc. which influence the return type of the method. On the other hand, there are operators like and, or, isIn, between, etc. that are applied to the actual query logic.
You start your query with a query subject keyword like findBy and then the fields of your entity (and optionally operators and more fields). You can even have nested fields like findByProviderName where your entity has a field provider which has a field name. If you define an invalid property or property path (e.g. findByProviderNamw), your Spring Boot application would fail on startup. You can find more about defining query methods in the official spring reference.
Spring Data using part tree JPA queries, than map them into SQL query by pre-defined parts.

How query method of Spring Data JPA generate query?

When a method is created under a specific rule of Spring Data JPA, a method that calls the corresponding query is created.
For example,
public interface CustomerJpaRepository implements JpaRepository<Customer, Long>{
public List<Customer> findByName(String name);
}
findByName() generate the query similar to one below.
select * from Customer where name = name;
I am curious about this principle. To be precise, I'm curious about the code that parses this method and turns it into a query.
I looked at the code of the SimpleJpaRepository class that implements JpaRepository, but could not find a clue. (Of course, there is a possibility that I did not find it).
In summary, when a method consisting of specific words is declared in JpaRepository, I am curious about the code that actually executes this method internally. More specifically, I'd like to see the code that makes this method works.
If there is no code to do this internally (I personally doubt it's possible...), I want to know how it is implemented in detail, if there is a link or material that explains the principle or internal process, please share related references.
The parsing logic for creating queries from spring-data repository method names is currently mainly declared in the package org.springframework.data.repository.query.parser.
Basically, a repository method name string is parsed into a PartTree, which contains Parts representing defined abstract query criteria.
The PartTree can then be used to create a more specific query object, e.g. with a JpaQueryCreator, or a RedisQueryCreator, depending on the type of repository.
I recommend you to check this Query Creation spring doc
It explains the rules of how the method convert into a query.

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.

What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
When I see the examples on the web, I see them there used kind of interchangeably.
What is the difference between them?
Why would you want to use one over the other?
JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.
Their main functions are:
CrudRepository mainly provides CRUD functions.
PagingAndSortingRepository provides methods to do pagination and sorting records.
JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.
Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.
Ken's answer is basically right but I'd like to chime in on the "why would you want to use one over the other?" part of your question.
Basics
The base interface you choose for your repository has two main purposes. First, you allow the Spring Data repository infrastructure to find your interface and trigger the proxy creation so that you inject instances of the interface into clients. The second purpose is to pull in as much functionality as needed into the interface without having to declare extra methods.
The common interfaces
The Spring Data core library ships with two base interfaces that expose a dedicated set of functionalities:
CrudRepository - CRUD methods
PagingAndSortingRepository - methods for pagination and sorting (extends CrudRepository)
Store-specific interfaces
The individual store modules (e.g. for JPA or MongoDB) expose store-specific extensions of these base interfaces to allow access to store-specific functionality like flushing or dedicated batching that take some store specifics into account. An example for this is deleteInBatch(…) of JpaRepository which is different from delete(…) as it uses a query to delete the given entities which is more performant but comes with the side effect of not triggering the JPA-defined cascades (as the spec defines it).
We generally recommend not to use these base interfaces as they expose the underlying persistence technology to the clients and thus tighten the coupling between them and the repository. Plus, you get a bit away from the original definition of a repository which is basically "a collection of entities". So if you can, stay with PagingAndSortingRepository.
Custom repository base interfaces
The downside of directly depending on one of the provided base interfaces is two-fold. Both of them might be considered as theoretical but I think they're important to be aware of:
Depending on a Spring Data repository interface couples your repository interface to the library. I don't think this is a particular issue as you'll probably use abstractions like Page or Pageable in your code anyway. Spring Data is not any different from any other general purpose library like commons-lang or Guava. As long as it provides reasonable benefit, it's just fine.
By extending e.g. CrudRepository, you expose a complete set of persistence method at once. This is probably fine in most circumstances as well but you might run into situations where you'd like to gain more fine-grained control over the methods expose, e.g. to create a ReadOnlyRepository that doesn't include the save(…) and delete(…) methods of CrudRepository.
The solution to both of these downsides is to craft your own base repository interface or even a set of them. In a lot of applications I have seen something like this:
interface ApplicationRepository<T> extends PagingAndSortingRepository<T, Long> { }
interface ReadOnlyRepository<T> extends Repository<T, Long> {
// Al finder methods go here
}
The first repository interface is some general purpose base interface that actually only fixes point 1 but also ties the ID type to be Long for consistency. The second interface usually has all the find…(…) methods copied from CrudRepository and PagingAndSortingRepository but does not expose the manipulating ones. Read more on that approach in the reference documentation.
Summary - tl;dr
The repository abstraction allows you to pick the base repository totally driven by you architectural and functional needs. Use the ones provided out of the box if they suit, craft your own repository base interfaces if necessary. Stay away from the store specific repository interfaces unless unavoidable.
Summary:
PagingAndSortingRepository extends CrudRepository
JpaRepository extends PagingAndSortingRepository
The CrudRepository interface provides methods for CRUD operations, so it allows you to create, read, update and delete records without having to define your own methods.
The PagingAndSortingRepository provides additional methods to retrieve entities using pagination and sorting.
Finally the JpaRepository add some more functionality that is specific to JPA.
I am learning Spring Data JPA. It might help you:
Below are the differences between CrudRepository and JpaRepository as:
CrudRepository
CrudRepository is a base interface and extends the Repository interface.
CrudRepository mainly provides CRUD (Create, Read, Update, Delete) operations.
Return type of saveAll() method is Iterable.
Use Case - To perform CRUD operations, define repository extending CrudRepository.
JpaRepository
JpaRepository extends PagingAndSortingRepository that extends CrudRepository.
JpaRepository provides CRUD and pagination operations, along with additional methods like flush(), saveAndFlush(), and deleteInBatch(), etc.
Return type of saveAll() method is a List.
Use Case - To perform CRUD as well as batch operations, define repository extends JpaRepository.
All the answers provide sufficient details to the question. However, let me add something more.
Why are we using these Interfaces:
They allow Spring to find your repository interfaces and create proxy objects for them.
It provides you with methods that allow you to perform some common operations (you can also define your custom method as well). I love this feature because creating a method (and defining query and prepared statements and then execute the query with connection object) to do a simple operation really sucks !
Which interface does what:
CrudRepository: provides CRUD functions
PagingAndSortingRepository: provides methods to do pagination and sort records
JpaRepository: provides JPA related methods such as flushing the persistence context and delete records in a batch
When to use which interface:
According to http://jtuts.com/2014/08/26/difference-between-crudrepository-and-jparepository-in-spring-data-jpa/
Generally the best idea is to use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
The JpaRepository should be avoided if possible, because it ties you repositories to the JPA persistence technology, and in most cases you probably wouldn’t even use the extra methods provided by it.
Recent update, spring-data-jpa 3.x
As of spring-data-jpa 3.x used with spring-boot 3.x and spring-core 6.x,
the structure and the hierarchy has been modified and seems to be more clear.
ListCrudRepository extends CrudRepository
ListPagingAndSortingRepository extends PagingAndSortingRepository
JpaRepository extends both ListCrudRepository, ListPagingAndSortingRepository.
So basically the new introduced interfaces of ListPagingAndSortingRepository, ListCrudRepository now represent the functionalities of the old interfaces but with returned types of List<T> while the remaining PagingAndSortingRepository, CrudRepository handle the return types of Iterable<T>
In the new (3.x) version the structure is the following:
In the past (before 3.0) many of the declared methods which returned List<T> have been directly declared inside the JpaRepository, but now with separate interfaces for those methods they have been extracted into ListPagingAndSortingRepository, ListCrudRepository.
So the structure before (3.x) was
I hope it is clear from the above schemas how the mentioned JpaRepository and CrudRepository have been modified in 3.x version.
In case you plan to migrate spring-data-jpa from 2.x into 3.x (would be necessary if you migrate from spring-boot 2.x to spring-boot 3.x) as illustrated in the above schemas you should expect to have breaking code in cases where you have used the PagingAndSortingRepository in your code, as in the past it was extending from CrudRepository and so your custom repository which extended directly the PagingAndSortingRepository already had access to the methods of CrudRepository. If this is the issue you should fix this by adapting your custom repository to also extend either the ListCrudRepository or the CrudRepository.
Crud Repository is the base interface and it acts as a marker interface.
JPA repository also extends the PagingAndSorting repository.
It provides all the method for which are useful for implementing pagination. Crud Repository doesn't provide methods for implementing pagination and sorting
You can refer - https://www.tutorialspoint.com/difference-between-crudrepository-and-jparepository-in-java#:~:text=Crud%20Repository%20is%20the%20base,acts%20as%20a%20marker%20interface.&text=JPA%20repository%20also%20extends%20the,for%20implementing%20pagination%20and%20sorting.

hibernate design issue

I have a custom filter for querying the database.
The API layer build the filter,send it to the dao layer and the dao execute the filter (filter.toCreteia()) and return List of results.
public interface IFilter {
Creteria toCriteria();
}
I want to make the DAO api to always as for filter + securityFilter in every method.
List getAll(IFilter filter, IFilter security); //each filter will be a creteria in the end
I end up inside the dao with 2 creterias: regular filter and security filter.
how can i render 2 creteias for returning one List of results?
Or, do you think I should use only one filter and in the API layer add security content to it?
Unless you want to go with an interceptor approach (e.g., have a SecurityInterceptor/proxy class which transparently modifies the criteria) I think it would be nicer design to have two separate filters.
Note that I don't see that it is possible to join two DetachedCriteria objects together. Although you can have a routine which based on two IFilter objects returns a single DetachedCriteria based e.g. on Restrictions.and(criterion1, criterion2).

Categories