-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Circular dependency between AttributeConverter and JPA #41156
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
Comments
We don't, as far as I can see and remember, do anything with |
Why would you inject JPA repositories into |
@TofanMihai, there's a typo (impkementing -> implementing) in your first comment. Kindly check. |
I honestly agree, but this is the way it has been implemented in the project I am currently working on. |
Thank you for your interest! |
Thanks for the sample. The cycle exists because Hibernate looks up
This reads to me like things used to work for you before upgrading to Spring Boot 3.x. I've downgraded the sample to Spring Boot 2.7.x (changing it to use I believe the change in behaviour is a side-effect of #24249. Bootstrapping the entity manager on a separate thread works around the circular dependency. You can restore this behaviour by setting That said, I would encourage you to update your converter so that it's a pure function as @quaff suggests. If you must keep the cycle but don't want to change the bootstrap mode (which can have broader consequences), you could also use @Converter
public class NameConverter implements AttributeConverter<String,String> {
private final Supplier<HumanRepository> humanRepository;
public NameConverter(ObjectProvider<HumanRepository> humanRepository) {
this.humanRepository = SingletonSupplier.of(() -> humanRepository.getObject());
}
@Override
public String convertToDatabaseColumn(String s) {
// Use the repository during conversion
humanRepository.get().count();
return s + "_converted";
}
@Override
public String convertToEntityAttribute(String s) {
// Use the repository during conversion
humanRepository.get().count();
return s.replace("_converted","");
}
} |
Upon migrating to Jakarta EE and upgrading to Spring 3.0+, we've noticed that the EntityManagerFactory defined in HibernateJpaConfiguration is causing a circular dependency issue with our converters.
Specifically, any custom converter implementing the AttributeConverter interface can no longer have JPA repositories injected into it because they form a cyclic dependency.
This issue can be encountered on any spring version > 3.0.0 which requires jakarta.
The text was updated successfully, but these errors were encountered: