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.
Comment From: christophstrobl
Sounds like this is related to spring-projects/spring-data-jpa#2753 / spring-projects/spring-framework#29796. Thoughts from your side @sdeleuze?
Comment From: sdeleuze
Hard to say without access to the sample.
@kartik-kaushik Could you please make the file accessible?
Comment From: kartik-kaushik
@sdeleuze sorry for that. I have made it accessible now. Kindly download and confirm. Awaiting your response. Thanks..
Comment From: sdeleuze
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?
Comment From: christophstrobl
@sdeleuze you're right - thanks for having a look! I think we should move the issue to spring-framework
.
Comment From: rishiraj88
Good issue to view and learn out of. Thanks to all.