spring-native icon indicating copy to clipboard operation
spring-native copied to clipboard

Injection of EntityManager does not work with Lombok

Open mnorsic opened this issue 3 years ago • 5 comments
trafficstars

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).

mnorsic avatar Apr 27 '22 12:04 mnorsic

Does this work on the JVM but fails on native?

mhalbritter avatar Jun 08 '22 12:06 mhalbritter

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.

spring-projects-issues avatar Jun 15 '22 12:06 spring-projects-issues

It works OK if I run it with mvn test -DspringAot=false

But when springAot is true, the tests fail.

mnorsic avatar Jun 20 '22 09:06 mnorsic

@schauder @christophstrobl Please make sure this use case is working with upcoming Spring Boot 3 support if you think it is a valid one.

sdeleuze avatar Jun 22 '22 14:06 sdeleuze

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.

mnorsic avatar Jul 04 '22 11:07 mnorsic

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.

sdeleuze avatar Jan 02 '23 11:01 sdeleuze