0

Am calling load method on hibernatetemplate as below.

Emp emp = (Emp) hibernateTemplate.load(Emp.class, 7369);

Emp.java

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;

@Entity
@Table(name = "emp")
public class Emp implements java.io.Serializable {

    @Id
    @Column(name = "empno")
    private Integer empno;

    @Column(name = "ename")
    private String ename;

    @Column(name = "job")
    private String job;

    @Column(name = "mgr")
    private Double mgr;

    @Column(name = "hiredate")
    private Date hiredate;

    @Column(name = "sal")
    private Double sal;

    @Column(name = "comm")
    private Double comm;

    @Column(name = "deptno")
    private Integer deptno;

}

It throws exception as below:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Any idea why it throws the above exception. If I use get method instead load, it works. Please share your thoughts on whats wrong there.

Thanks in advance.

mabi
  • 5,279
  • 2
  • 43
  • 78
  • 3
    100 $ that the exception is not thrown by load(), but by another method call that comes later, when the session is closed. Post your code, and the full stack trace of the exception. And read the javadoc of load() and get(): the explanation is in the documentation. – JB Nizet Dec 22 '13 at 12:48
  • 1
    I'm not a spring user but the [interwebs](http://stackoverflow.com/questions/8977121/advantages-of-using-hibernate-callback) point out that `HibernateTemplate` closes active sessions before starting a new one. You might have to deal with detached objects from an earlier call. – mabi Dec 22 '13 at 12:52
  • 1
    If you are only learning Spring, then please note that the whole `HibernateTemplate` is deprecated - there is no `HibernateTemplate` equivalent for Hibernate 4.0 + – Boris Treukhov Dec 22 '13 at 15:27

1 Answers1

1

When you execute load it will create proxy object(it don't fire select statement to the database and get the data). After that the first time you try to access some property of the object the proxy will try to select from the DB. But this can only happen inside hibernate transaction. Try to use get instead of load or use the proxy object inside session.

If you're using Spring to manage your transactions, make sure that the use of the proxy is inside a @Transactional method. E.g. This will work

   @Transactional
     public void getEmp(long id){
        Emp emp = (Emp) hibernateTemplate.load(Emp.class, 7369);//the proxy will be created
        emp.getSomeProperty();//the select statement will be executed
     }

This will throw the exception:

 @Transactional
     public Emp getEmp(long id){
        Emp emp = (Emp) hibernateTemplate.load(Emp.class, 7369);//the proxy will be created
        return emp;
     }
...
public void someNonTransactionalMethod(){
   Emp emp = empServce.getEmp(1);
   emp.getSomeProperty();//hibernate will try to select, but it's not in transaction
}

You can make someNonTransactionalMethod transactional, but take care that this will need additionl connections from the DB.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • How to add proxy object inside session? Hibernatetemplate handles session automatically. Generally, we don't have control on sessions when using hibernatetemplate. – user3127033 Dec 23 '13 at 02:31
  • I have added @Transactional annotation. Still it throws exception. – user3127033 Dec 31 '13 at 08:36
  • LazyInitializationException again? For @Transactional to work it need to be in Spring bean. Is the bean where the exception is thrown taken from the Spring context? – Evgeni Dimitrov Dec 31 '13 at 08:49
  • Yes, the class am using declared as a bean in spring context file. In this bean, am using @Transactional annotation. Still it throws LazyInitializationException. – user3127033 Jan 02 '14 at 07:59