Spring Data JPA – “could not initialize proxy – no Session” – With Methods marked as transactional

We Are Going To Discuss About Spring Data JPA – “could not initialize proxy – no Session” – With Methods marked as transactional. So lets Start this Java Article.

Spring Data JPA – “could not initialize proxy – no Session” – With Methods marked as transactional

  1. Spring Data JPA – “could not initialize proxy – no Session” – With Methods marked as transactional

    If you would like to keep Lazy Load and you are using Spring Boot, just add the config below in your application.properties:
    spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

  2. Spring Data JPA – “could not initialize proxy – no Session” – With Methods marked as transactional

    If you would like to keep Lazy Load and you are using Spring Boot, just add the config below in your application.properties:
    spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

Solution 1

If you would like to keep Lazy Load and you are using Spring Boot, just add the config below in your application.properties:

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

Original Author Joao Luiz Cadore Of This Content

Solution 2

The lazy loading can be kept, without the setting the enable_lazy_load_no_trans parameter. The simplest solution I found was @NamedEntityGraph while using Spring Data JPA. https://www.baeldung.com/spring-data-jpa-named-entity-graphs

The downside was I could not have more than one collection in the @NamedEntityGraph. Adding a second collection resulted in an exception org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags:

Therefore, if you don’t want to use the anti-pattern, and only have one collection you are trying to load, the @NamedEntityGraph and @EntityGrpah works with Spring Data JPA.

Original Author Bill Snee Of This Content

Solution 3

I had the same exception and the issue was, I had an entity where the mapping was missed

@Entity
Class A {
}

@Entity
Class B {

   private A a;
}

Solution

@Entity
Class B {

  @OneToOne
  @MapsId
  private A a;
}

Original Author Jafar Karuthedath Of This Content

Solution 4

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true it is an anti-pattern and highly recommend to avoid to use it.

could not initialize proxy exception will occur often when child class contains
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) in your relationship.

I recommend to use fetch = FetchType.EAGER in your relationship instead of using LAZY. It is not the best way but it is far better than using anti-pattern.

Original Author NafazBenzema Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment