
How to make Hibernate Lazy initialization to work (within Spring Boot)?
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at com.example.domain.Employee_$$_jvstb7e_2.toString(Employee_$$_jvstb7e_2.java) ~[bin/:na]
at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_74]
at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_74]
at com.example.domain.Phone.toString(Phone.java:13) ~[bin/:na]
at com.example.SpringBootQuerydslTestBean$1.run(SpringBootQuerydslTestBean.java:49) ~[bin/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:806) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
... 6 common frames omitted
public class Phone {
...
@ManyToOne(fetch=FetchType.LAZY)
private Employee owner;
P.S.
Some advises to use FetchType.EAGER like in org.hibernate.LazyInitializationException: could not initialize proxy - no Session,
but making Lazy loading work (within Spring Boot, Spring Data JPA) is exactly my goal.
toStringthat doesn't include the back reference. The issue is you are logging something (I guess)... Again potentially dangerous as when it would work you probably would run into a stack overflow exception due to Pone referencingEmployeewhich has atoStringprobably referencingPhoneagain and gain and again.