How to configure with non-primary JPA Entity Manager
Closed this issue · 5 comments
Hi Stephan, I am trying to incorporate your example into an existing spring boot application to secure some REST services. I have set up 2 separate persistence contexts, one for the application (primary) and one for the spring security from this project.
I am getting an error when I run a unit test saying that it could not instantiate a bean because it can't find the primary entity manager.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openEntityManagerInViewInterceptor'
defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration$JpaWebMvcConfiguration.class]:
Initialization of bean failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: more than one 'primary' bean found among
candidates: [entityManagerFactory, entityManagerFactorySecurity]
As of right now I have both defined as primary but once I define the main app context as primary, I am afraid the security will not work or attempt to access the main persistence context. They both use the same database but the repository classes are in separate packages.
What do I need to do to setup the security pieces to use the 'entityManagerFactorySecurity' entity manager factory?
Thanks for you help in advance.
Miguel.
I haven't done that yet, too. But maybe this links could help you:
Thanks, I have seen and followed those links for setting up my two entity manage factories. Problem was that both were set as primary. I set the entityManagerFactorySecurity as primary and it works fine. What I found is that the Repository objects need a @transactional annotation with the name of the transaction manager bean to make sure that they use a particular transaction manager, these links don't show that except for the baeldung.com one but that one defines the transaction manager in the @test methods rather than the @repository class. I still don't know how to tell openEntityManagerInViewInterceptor to use the proper entity manager factory, it seems the configuration pieces didn't take this scenario into account or I am not finding enough information about it. It is working fine having the security configuration pieces as @primary so I am ok for now.
Thanks.
Miguel.
Thanks for sharing this infos. I recommend to use Stackoverflow or ask in the Spring forum for this special problems.
No problem, glad if my solution can help others. By the way, thank you very much for this demo app, it works great :).
You're welcome ;)