Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processing several @PersistenceContext injection points on the same class with AOT generates code that does not compile #30437

Closed
kartik-kaushik opened this issue Apr 27, 2023 · 6 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Milestone

Comments

@kartik-kaushik
Copy link

Spring native app issue with PersistenceContext annotation

we have application with spring boot 3.0.4 which connect to oracle DB.

application build fine normally but for native application is failling if we have more than one PersistenceContext annotation

error: [ERROR] variable field is already defined in method apply(org.springframework.beans.factory.support.RegisteredBean,com.codingknownsense.oraclespringboot.dao.DBSyncDAOImpl)

Sample app location:
https://drive.google.com/file/d/1d373KxaTa73IHkN6rU_NerhXvTBToM04/view?usp=share_link

Kindly help in the same.

@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, 2023
@christophstrobl
Copy link
Member

Sounds like this is related to spring-projects/spring-data-jpa#2753 / #29796.
Thoughts from your side @sdeleuze?

@sdeleuze
Copy link
Contributor

Hard to say without access to the sample.

@kartik-kaushik Could you please make the file accessible?

@kartik-kaushik
Copy link
Author

@sdeleuze sorry for that. I have made it accessible now. Kindly download and confirm.
Awaiting your response. Thanks..

@sdeleuze
Copy link
Contributor

Looks a bit different I think.

If I take the repro, I have a repository bean with 2 persistence contexts:

public class DBSyncDAOImpl {

	@PersistenceContext(unitName = "CUSTDB")
	@Qualifier("entityManagerFactory")
	private EntityManager customDBEntityManager;

	@PersistenceContext(unitName = "EMS")
	@Qualifier("customDBEntityManagerFactory")
	private EntityManager customDBEntityManager2;

	// ...
}

And the injection code generated is:

public static DBSyncDAOImpl apply(RegisteredBean registeredBean, DBSyncDAOImpl instance) {
    Field field = ReflectionUtils.findField(DBSyncDAOImpl.class, "customDBEntityManager");
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, instance, getCUSTDBEntityManager(registeredBean));
    Field field = ReflectionUtils.findField(DBSyncDAOImpl.class, "customDBEntityManager2");
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, instance, getEMSEntityManager(registeredBean));
    return instance;
  }

Which does not compile because, well, there are 2 variable field with the same name. So I think a fix is potentially needed needed, but more to use different variable names in case there are multiple entity managers to inject.

@christophstrobl How related does it look like from your POV?

@gregturn gregturn added the theme: aot An issue related to Ahead-of-time processing label May 1, 2023
@christophstrobl
Copy link
Member

@sdeleuze you're right - thanks for having a look! I think we should move the issue to spring-framework.

@christophstrobl christophstrobl removed their assignment May 2, 2023
@snicoll snicoll transferred this issue from spring-projects/spring-data-jpa May 6, 2023
@snicoll snicoll added the in: data Issues in data modules (jdbc, orm, oxm, tx) label May 6, 2023
@snicoll snicoll changed the title Issue with @PersistenceContext in native app Processing several @PersistenceContext injection points on the same class with AOT generates code that does not compile May 10, 2023
@snicoll snicoll self-assigned this May 10, 2023
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 10, 2023
@snicoll snicoll added this to the 6.0.9 milestone May 10, 2023
@rishiraj88
Copy link

Good issue to view and learn out of. Thanks to all.

mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
This commit adapts the generated code so that each injection point has
a dedicated namespace in the form of a private method. That prevents
the same variable to be reused twice which lead to a compilation failure
previously.

Closes spring-projectsgh-30437
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Projects
None yet
Development

No branches or pull requests

7 participants