We are working on web application using Spring data JPA with hibernate.
In the application there is a field of compid in each entity. Which means in every DB call (Spring Data methods) will have to be checked with the compid.
I need a way that, this "where compid = ?" check to be injected automatically for every find method. So that we won't have to specifically bother about compid checks.
Is this possible to achieve from Spring Data JPA framework?
May be Hibernate‘s annotation
@Where
will help you. It adds a given condition to any JPA queries. For exampleMore info: 1, 2
Like other people have said there is no set method for this
One option is to look at Query by example - from the spring data documentation -
So you could default compid in the object, or parent JPA object
Another option is a custom repository
Agree with Abhijit Sarkar.
You can achieve your goal hibernate listeners and aspects. I can suggest the following : create an annotation
@Compable
(or whatever you call it) to mark service methods create CompAspect which should be a bean and@Aspect
. It should have something like this3)Also you need to provide hibernate filters. If you use annotation this can look like this:
So your condition
where compid = ?
will be@Service
method belowThat's basically it for Selects, For updates/deletes this scheme requires an
EntityListener
.