New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add way to enable Session filter for Hibernate 5+ [SPR-16518] #21061
Comments
Juergen Hoeller commented Oliver Drotbohm, what's your take on this from a Spring Data perspective? |
Dominik Bartholdi commented Juergen Hoeller is there any chance to make this easier? |
Hi, I'd like to note that AOP approach (the first example from Dominik) with repositories doesn't work for So it seems that the only option is aspectj load time weaving. And this is very cumbersome for spring boot applications. Are there any other ways to intercept sessions and enable filters? |
See #25125 |
Hi @jofatmofn, this is just a normal spring
Your entities should be annotated with proper field annotations:
And I think that's it (if I didn't forget anything). Unfortunately, this is not customizable by a repository. But usually, repositories correspond to some particular entities, so you have to add filters only to those entities, which you are interested in. I think it's fine to always set a filter in the If you really want to customize based on a repository, I believe you have to go AOP way, with load time weaving and java agent. |
Hi @bugy, Thanks for the quick response. My objective is to achieve Multi-tenancy. I think I am badly missing a key point - Are these solutions suitable for shared database, shared schema scenario? I don't intend to hijack this thread, if it is not appropriate. Could you please confirm this point alone. Thanks.
|
Hi @jofatmofn, I'm also using it for multitenancy, with a shared DB and schema. |
Thanks @bugy, I have raised a SO question for my issue at https://stackoverflow.com/questions/62387223/why-createentitymanager-is-called-before-my-rest-controller-method. |
Hi @jofatmofn, I have implemented and documented this in a standalone Spring Boot application except the TenantId is applied through a received JWT token. But the same concept can be applied with your tenantId. This project is built to tackle the discriminator based multi tenancy problem. |
Dominik Bartholdi opened SPR-16518 and commented
in the context of #5615, setFilterName and setFilterNames methods have been added to HibernateAccessor to support enabling Hibernate Filters for Hibernate 3. Unfortunate there seems no equivalent way to enable Hibernate Filters when using Spring Data Repositories with Hibernate 5.
Sure, I can create a pointcut that matches all repositories:
But this means the aspect is executed way to many times - e.g. if I have a service calling multiple repository methods.
Another way would be to define a custom annotation (e.g.
@EnableTenantFilter
) and annotate service methods with it to enable the filter based on a Pointcut specific for this Annotation and do the same as in above, but this could lead to the same issue as above.A nicer way would be to define a Pointcut for org.hibernate.SessionBuilder.openSession, but as SessionBuilder is not exposed as a Bean, but this requires Load Time Weaving :(
Also DATACMNS-293 would provide a way to implement such a filter (even independent of Hibernate), but it does not seem to be merged anytime soon :(
A way to ease this, would be some kind of a listener that is called whenever a new Hibernate Session is created and allows me to enable the Filter based on my criteria at that point. An important point is that I need to be able to pass parameters to the filter, otherwise it does not make a lot of sense.
Maybe I have missed something and this already exists...
No further details from SPR-16518
The text was updated successfully, but these errors were encountered: