Why createEntityManager is called before my REST controller method - spring-boot

This is more of a Spring JPA (Hibernate) related issue than AspectJ one, I believe.
Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE, Hibernate v5.4.9.Final, Spring Data Repositories
I am trying to implement Multi-tenancy in my existing single tenant application.
The tenant is chosen by the users via front-end. The tenant id is
stored in HttpSession.
An advice for Rest Controller methods, reads
the tenant id and keeps it in a ThreadLocal variable.
Now I have to enable a Hibernate Filter using the tenant id. For this, an AspectJ advice for my Repository (interface extends JpaRepository) methods work, but gets executed for each repository method call from Service.
This post is about the following (identical) implementations where the advice for enabling Hibernate Filter is fired before the advice for reading the tenant id from HttpSession. How do I correct the order? The advice for reading the tenant id does not make any repository calls.
#AfterReturning(pointcut = "execution(* javax.persistence.EntityManagerFactory+.createEntityManager(..))", returning = "entityManager")
Ref: https://github.com/spring-projects/spring-framework/issues/25125 (This thread contains a non-AOP way also - post-processing transactional EntityManager instances - That gets triggered only when the Spring Boot starts)
#AfterReturning(pointcut="bean(entityManagerFactory) && execution(* createEntityManager(..))", returning="retVal")
Ref: Access to Session using Spring JPA and Hibernate in order to enable filters
I tried setting the application property spring.jpa.open-in-view=false, but that didn't help.

Related

Transaction - Springboot(JHipster) - RestService: how to

I have a microservice created with JHipster(SpringBoot+JPA) exposing a rest api.
During a save operation over an Entity I need to manage the transaction because I must execute another update over the DB (using other Entities).
How can I do this?
Using the tradictional approach (JDBC) I got the connection and create a transaction over it, make all the queries and finally close the transaction(commit/rollback).
With JPA I have an Entity, but I find no way to specify to begin/end(manage) the transaction programmatically.
You have many alternatives, here are few ones:
define a service (a class annotated with #Service) and annotate with #Transactional the public method that implements your logic
manage transaction manually through the EntityManager injected into your service class constructor
create a custom Repository
Check the Spring docs

How does a spring converter get a transactional scope?

I was wondering, how a spring converter implementation gets an transactional scope.
Having a Converter which is used to convert a path placeholder (entity id) in an (rest) controller to the Entity itself. The Entity thus is loaded via Hibernate from the database.
For the details:
spring boot 2.3.2
A rest controller endpoint which triggers the converter to expand a path-placeholder (prior executing the rest-endpoint method body)
If in question, the controller method is not annotated #Transactional either
Question:
How (since we disabled OSIV) does this Converter get it's Transactional scope if the convert neither the method/nor the converter class is annotated using Transactional nor the convert method does custom transaction handling?
If you mean how Spring Data repositories get a transaction, the answer is all repository methods are transactional by default.

Spring Boot get Authentication Principal

I'm using Spring Boot Parent version 1.5.16.
I need to use current user information and now I use #AuthenticationPrincipal annotation.
But I have to write this annotation for each method. I think this is not a best practice because I have more than 200+ methods. Is there any other suggestion to inject Authentication Principal globally or class level?

Does spring-data by default always use the same persistence context within the same request?

An example is if my spring controller has two Autowired services, and both services have a PersistenceContext also controled by spring (and i'm doing nothing more), will both share the same context in every request by default?
No, they will use different ones. A persistence context (EntityManager) is defined to be a thread-bound concept in JPA. Thus each request will see a fresh instance of EntityManager for each new request.
For a singleton Spring component that gets an EntityManager injected, Spring will autowire you a proxy instance so that it can easily exchange the backing instance. This a core Spring container feature and doesn't need anything in Spring Data JPA (see the documentation here).

Spring Boot/JPA not persisting with service layer

I'm using Spring boot along with Spring Data JPA.
I'm stucking in an odd scenario when saving a new entity.
Unsing method from extended CrudRepository class, all works as expected.
If I inject via #Autowired the CrudRepository interface in my service layer, the method still works, but nothing is persisted on db.
The returned object from 'save' method seems ok, since I get an always increasing ID value.
Suggestions?
Cheers
FB
check whether we are populating the data properly in your bean and check it before passing to save method of spring data jpa

Resources