2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

org.hibernate.LazyInitializationException - could not initialize proxy in hibernate

I am using hibernate as a ORM tool for my application. When I made any hibernate call to database I am getting the below exception.

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at sei.persistence.wf.entities.Element_$$_jvstc68_47.getNote(Element_$$_jvstc68_47.java)
    at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)
    at JSON_to_XML.main(JSON_to_XML.java:84)


The exception is quite new for me and not getting any proper solution to get relieve from this. Please suggest!

userimage

I didn't got any hibernate version or the pojo declaration means, is your mapping based on annotation or mapping file from your description?

And if you are using hibernate relationships then its a common exception for all. I am assuming you declared any relationships on your mapping configuration with LAZY loading.

So just follow below solution to recover from your LazyInitializationException issue.


For Annotation Based :  Set from "fetch=FetchType.LAZY" to "fetch=FetchType.EAGER" will fixed the issue.

For Mapping Based : Just add lazy="false" (default to true) with your relationship configuration.


Answer is