Comments (6)
We don't, as far as I can see and remember, do anything with AttributeConverter
s in Spring Boot so it's not clear to me why the cycle exists. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
from spring-boot.
Why would you inject JPA repositories into AttributeConverter
? IMO AttributeConverter
should be pure function.
from spring-boot.
@TofanMihai, there's a typo (impkementing -> implementing) in your first comment. Kindly check.
from spring-boot.
Why would you inject JPA repositories into
AttributeConverter
? IMOAttributeConverter
should be pure function.
I honestly agree, but this is the way it has been implemented in the project I am currently working on.
Trying to extract the logic and transform the converter into a service would be too cumbersome as it is currently applied on numerous entities and would mean modifying hundred of methods, trying to apply the converter logic after each load and before each persist.
from spring-boot.
We don't, as far as I can see and remember, do anything with
AttributeConverter
s in Spring Boot so it's not clear to me why the cycle exists. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Thank you for your interest!
I have created a small demo reproducing the issue and attached it to this reply.
from spring-boot.
Thanks for the sample.
The cycle exists because Hibernate looks up Converter
implementations through its BeanContainer
abstraction and Spring Framework's SpringBeanContainer
implementation.
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.
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 javax.persistence
), and the same circular reference exists. I have to go back to 2.4.0 before the application starts successfully.
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 spring.data.jpa.repositories.bootstrap-mode=deferred
. The Spring Boot 3.2.6-based application starts successfully with this property in place.
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 ObjectProvider<HumanRepository>
to inject the dependency into your converter. You can use Framework's SingletonSupplier
so that the lookup of the repository bean is still lazy but it only performed once:
@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","");
}
}
from spring-boot.
Related Issues (20)
- Closing org.springframework.web.context.support.GenericWebApplicationContext issue during running Test class HOT 6
- Configuration property parsing list has unexpected behavior HOT 2
- @RedisHash timeToLive is a long, which is not compatible with the use of SPeL HOT 1
- Run Docker-based tests serially
- Run Docker-based tests serially
- Combine tests for standard and Bitnami images into single classes
- Replace RFC 7807 by RFC 9457 in property documentation HOT 2
- Combine tests for standard and Bitnami images into single classes
- Rework fix for #21992 to avoid polluting Gradle Module Metadata with mavenOptional… variants
- SpringBoot Liquibase Support of rollback automatically HOT 1
- Improve classpath index documentation for reproducible builds HOT 3
- Tracing propagation does not work in ControllerAdvice with async controllers since 3.2.7 HOT 6
- Polish gh-40023
- Rework fix for #21992 to avoid polluting Gradle Module Metadata with mavenOptional… variants
- Rework fix for #21992 to avoid polluting Gradle Module Metadata with mavenOptional… variants
- Update Kotlin DSL examples that configure the environment of bootBuildImage to be additive
- Update Kotlin DSL examples that configure the environment of bootBuildImage to be additive
- Make buildSrc's custom tasks consistently abstract
- Make buildSrc's custom tasks consistently abstract
- Make buildSrc's custom tasks consistently abstract
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-boot.