Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Injection of EntityManager does not work with Lombok #1597

Closed
mnorsic opened this issue Apr 27, 2022 · 6 comments
Closed

Injection of EntityManager does not work with Lombok #1597

mnorsic opened this issue Apr 27, 2022 · 6 comments
Labels
for: external-project For an external project and not something we can fix type: compatibility Native image compatibility issue

Comments

@mnorsic
Copy link

mnorsic commented Apr 27, 2022

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 (#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 #1372 with
@PersistenceContext private EntityManager entityManager;

EDIT2: Yes, I'm aware of spring-projects/spring-framework#15076, but I don't have multiple persistence contexts, and Spring Data JPA allows constructor injection (spring-projects/spring-data-jpa#847).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 27, 2022
@mhalbritter
Copy link
Contributor

Does this work on the JVM but fails on native?

@mhalbritter mhalbritter added the status: waiting-for-feedback We need additional information before we can continue label Jun 8, 2022
@spring-projects-issues
Copy link

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 spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Jun 15, 2022
@mnorsic
Copy link
Author

mnorsic commented Jun 20, 2022

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

But when springAot is true, the tests fail.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Jun 20, 2022
@sdeleuze
Copy link
Contributor

@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 sdeleuze added type: compatibility Native image compatibility issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jun 22, 2022
@sdeleuze sdeleuze added this to the Backlog milestone Jun 22, 2022
@mnorsic
Copy link
Author

mnorsic commented Jul 4, 2022

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.

gtiwari333 added a commit to gtiwari333/spring-boot-blog-app that referenced this issue Oct 22, 2022
image generates fine.. but fails because of EntityGraph

Revisit after these two issues are fixed
- spring-attic/spring-native#1729
- spring-attic/spring-native#1728

Found a temp workaround for EntityManager injection with lombok
spring-attic/spring-native#1597
gtiwari333 added a commit to gtiwari333/spring-boot-blog-app that referenced this issue Dec 5, 2022
gtiwari333 added a commit to gtiwari333/spring-boot-blog-app that referenced this issue Dec 5, 2022
* attempting to generate native image

image generates fine.. but fails because of EntityGraph

Revisit after these two issues are fixed
- spring-attic/spring-native#1729
- spring-attic/spring-native#1728

Found a temp workaround for EntityManager injection with lombok
spring-attic/spring-native#1597

* will get this corrected in master branch

* Merge branch 'master' into boot3-native

# Conflicts:
#	src/main/java/gt/app/domain/AppUser.java

* graalvm 22.3+ is required

* h2 required at compile time for graalvm native compile

* native image WIP

* now it works !

* now it works !

* spring-attic/spring-native#1597 is resolved in   spring boot 3

* spring-projects/spring-data-jpa#2681 is fixed

* spring-projects/spring-data-jpa#2681 is fixed

* spring-projects/spring-data-jpa#2681 is fixed
@sdeleuze
Copy link
Contributor

sdeleuze commented Jan 2, 2023

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 sdeleuze closed this as completed Jan 2, 2023
@sdeleuze sdeleuze removed this from the Backlog milestone Jan 2, 2023
@sdeleuze sdeleuze added the for: external-project For an external project and not something we can fix label Jan 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
for: external-project For an external project and not something we can fix type: compatibility Native image compatibility issue
Development

No branches or pull requests

4 participants