Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Old Issue, but not finding a solution: org.hibernate.LazyInitializationException

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a spring-hibernate code which does the following two steps

1. Save an Object to database
2. Retrieve the object saved and display the details of that values saved


To do the above two steps in a row , I saved the getHibernateTemplate().saveOrUpdate(obj);. To my knowledge this calls the hibernate saveorupdate() method internally and saves it to the database. After saving to the database it closes the Hibernate session.

In step two, to retrieve the object from the database, I executed return (Employee)getHibernateTemplate().load(Employee.cla ss, id);. To retrieve data from database the earlier session is closed, now it says, org.hibernate.LazyInitializationException: could not initialize proxy - no Session

To avoid the above situation, I placed both the statements in a single method and declared it as a transaction (@Transactional(propagation=Propagation.REQUIRED , rollbackFor=Exception.class). Still the same issue. I am pasting my code below, please kindly look into it. Thanks in advance.

EmployeeDao


App.java


Output


Bean retrived from bean factory ...
Data has been saved
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session

 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use of Hibernate template is not recommended. You should inject Hibernate session factory in you DAO & get Hibernate session by calling the getCurrentSession() method on session factory. This will get current session or create new session depending on transaction settings.

Can you share spring configuration file, you current issue could be caused by incorrect transaction configurations.
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for HibernateTemplate, which I agree shouldn't be used with the current versions of Hibernate. But I don't see any other Spring. So that one method in your main class has @Transactional. But in order for it to be transactional it would have to be defined as a Spring bean in the configuration, which I don't think you want to put the class that has your public static void main as a Spring bean.

Can you post your Spring-Hibernate.xml file.

Also, if you are just learning Spring, I highly recommend using the latest version to learn from. Based on the code I see in your main method, you are using the older style of Spring, mostly from Spring 1.x days. We are at Spring 3.x today.

Mark
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!