Giter Site home page Giter Site logo

Comments (6)

wilkinsona avatar wilkinsona commented on July 21, 2024

We don't, as far as I can see and remember, do anything with AttributeConverters 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.

quaff avatar quaff commented on July 21, 2024

Why would you inject JPA repositories into AttributeConverter ? IMO AttributeConverter should be pure function.

from spring-boot.

Jayanta0123 avatar Jayanta0123 commented on July 21, 2024

@TofanMihai, there's a typo (impkementing -> implementing) in your first comment. Kindly check.

from spring-boot.

TofanMihai avatar TofanMihai commented on July 21, 2024

Why would you inject JPA repositories into AttributeConverter ? IMO AttributeConverter 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.

TofanMihai avatar TofanMihai commented on July 21, 2024

We don't, as far as I can see and remember, do anything with AttributeConverters 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.

circular_dependency_demo.zip

from spring-boot.

wilkinsona avatar wilkinsona commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.