spring-native
spring-native copied to clipboard
Injection of EntityManager does not work with Lombok
Using Spring Boot 2.6.7 (Web/JPA/Lombok) with Spring Native, when injecting EntityManager via a constructor injection, I'm getting a following error:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: more than one 'primary' bean found among candidates: [org.springframework.orm.jpa.SharedEntityManagerCreator#0, org.springframework.orm.jpa.SharedEntityManagerCreator#1]
I have seen another issue opened with the same cause (https://github.com/spring-projects-experimental/spring-native/issues/1372), but adding @PersistenceContext annotation did not help (IMHO this annotation is not relevant because this is a constructor-based injection).
The sample app is on https://github.com/mnorsic/spring-native-jpa-demo, I have taken data-jpa sample and added a FooService, so it should be easily reproducible.
EDIT: it works when using proposed solution from beforementioned ticket https://github.com/spring-projects-experimental/spring-native/issues/1372 with
@PersistenceContext private EntityManager entityManager;
EDIT2: Yes, I'm aware of https://github.com/spring-projects/spring-framework/issues/15076, but I don't have multiple persistence contexts, and Spring Data JPA allows constructor injection (https://github.com/spring-projects/spring-data-jpa/issues/847).
Does this work on the JVM but fails on native?
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
It works OK if I run it with
mvn test -DspringAot=false
But when springAot is true, the tests fail.
@schauder @christophstrobl Please make sure this use case is working with upcoming Spring Boot 3 support if you think it is a valid one.
Forgot to mention that the same error occurred in a result native image (both when building with Buildpacks and Native Build Tools). So, the error is not related to test only.
Spring Native is now superseded by Spring Boot 3 official native support, see the related reference documentation for more details.
As a consequence, I am closing this issue, and recommend trying your use case with latest Spring Boot 3 version. If you still experience the issue reported here, please open an issue directly on the related Spring project (Spring Framework, Data, Security, Boot, Cloud, etc.) with a reproducer.
Thanks for your contribution on the experimental Spring Native project, we hope you will enjoy the official native support introduced by Spring Boot 3.