Announcement Announcement Module
Collapse
No announcement yet.
Old Issue, but not finding a solution: org.hibernate.LazyInitializationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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 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
    Code:
    public class EmployeeDao {
    	private HibernateTemplate hibernateTemplate;
    	private static Session session;
        public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
            this.hibernateTemplate = hibernateTemplate;
        }
    
        public HibernateTemplate getHibernateTemplate(){
            return hibernateTemplate;
        }
    
        
        
        public Employee getEmployee(final String id){        
        	return (Employee)getHibernateTemplate().load(Employee.class, id);
        }
    
    
        public void saveOrUpdate(final Employee employee){
        	getHibernateTemplate().saveOrUpdate(employee);
        }
    }
    App.java
    Code:
    public class App {
    
    	public static void main(String[] args) {
            Resource resource = new FileSystemResource("Spring-Hibernate.xml");
                BeanFactory factory = new XmlBeanFactory(resource);
                System.out.println("Start ...");
                Employee employee = new Employee();
                employee.setId("12345");
                employee.setName("ABC");
                employee.setAge(20);
                employee.setSalary(15000);
                System.out.println("Values are set to POJO ...");
                EmployeeDao employeeDao = (EmployeeDao)factory.getBean("employeeDao");
                System.out.println("Bean retrived from bean factory ...");
                
                transactMethods(employee, employeeDao);
    	}
    	
    	@Transactional(propagation=Propagation.REQUIRED , rollbackFor=Exception.class)
        public static void transactMethods(Employee employee, EmployeeDao employeeDao)
        {
        	employeeDao.saveOrUpdate(employee);
        	System.out.println("Data has been saved");
        	Employee empResult = employeeDao.getEmployee("12345");       
            System.out.println("Employee results " +empResult);
        }
    
    }
    Last edited by venkataprasad; Sep 12th, 2011, 04:37 AM.

  • #2
    I have googled the web a lot. All the examples I am finding are related to Hibernate. To solve this issue from Spring point of view I am finding the solution which I have implemented.@Transactional(propagation=Propagation.REQUIRED , rollbackFor=Exception.class)

    Comment


    • #3
      1) Don't use HibernateTemplate it isn't recommended anymore since 2007
      2) Don't use load use get
      3) Use the OpenSessionInViewFilter or Interceptor to keep the session open for view rendering.

      Currently your session gets closed after the @Transactional method (which basically isn't transactional because it is static and static methods aren't proxies). Next to that use an ApplicationContext instead of a BeanFactory (read chapter 3 of the reference guide to find out what the differences are). Next to that I suggest a read of the aop chapter especially the chapter which explains proxies.

      Comment


      • #4
        Thanks Marten.

        Comment


        • #5
          Marten, One quick question.
          Don't use HibernateTemplate it isn't recommended anymore since 2007
          Could you please tell me what you would suggest to use in place of HibernateTemplates. Thanks in advance.

          Comment


          • #6
            Do you mean HibernateDAOSupport with AOP interceptor

            Comment


            • #7
              To my observation even HibernateDAOSupport internally uses HibernateTemplate

              Comment


              • #8
                Which is also not recommended simply use the session factory and use getcurrentsession (I suggest a read of the reference guide)...

                Comment

                Working...
                X