2

In Employee and Phone example

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

Java code

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.

3
  • have you tried current_session_context_class --> thread? Commented Jun 23, 2016 at 7:15
  • Do you have repository.impl classes?? You can fetch data from JPA query. Commented Jun 23, 2016 at 7:21
  • 1
    Create a proper toString that 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 referencing Employee which has a toString probably referencing Phone again and gain and again. Commented Jun 23, 2016 at 7:24

3 Answers 3

3

Thanks to M. Deinum for hint.

This particular case was solved by adding toString() in Employee to re-define over Lombok toString()

@Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }

also specifying exactly Fetch type

code at https://github.com/paulvi/spring-boot-querydsl

Sign up to request clarification or add additional context in comments.

1 Comment

You could also use Lombok and just explicitly exclude the employee field from Phone which should do the trick.
1

This is happening because of the Lombok instruction @Data, instead of it, use @Getter and @Setter, Follow javadocs from @Data:

Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.

The instruction @ToString generate a implementation of toString method for entire class, so in your JPA return needs to have data for all the object.

Comments

0

I think you are trying to get the childs from the proxy when the current session is detached , you can get the lazy childs in the same session .

1 Comment

How to control sessions(that are Hibernate things) from Spring Boot that uses Spring Data repositories over JPA?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.