1

Hibernate emits a lot of lazy initialization exceptions, but EclipseLink does not. When an entity is detached, and trying to access an uninitialized attribute, the exception will occur. But it is not an issue with EclipseLink. How does EclipseLink handle it differently?

e.g.

public class Employee {

     @OneToMany(fetch=FetchType.LAZY)
     private List<Task> tasks; 
}

Employee employee = em.find(Employee.class, 1);
em.detach(employee);
employee.getTasks();   // exception here
eastwater
  • 4,624
  • 9
  • 49
  • 118

1 Answers1

1

I haven't used EclipseLink but having read up on the cause of the LazyInitializationException for Hibernate I can tell that EclipseLink has a different way treating lazy loaded relations than Hibernate.

You can find some answers here regarding this here:

and a straight up Google search will show even more results.

The main idea is that EclipseLink will allow you to access lazy relations even after the EntityManager has been closed. As far as I can tell from reading up this is an EclipseLink specific behavior and part of JPA. Hibernate on the other hand requires a session to be open in order to initialize a proxy and access the lazy relation.

If one is not present then the exception will be thrown. If you're using Spring you can always annotate the methods needing acces to lazy relations with @Transactional to make sure that a session will be available for you.

akortex
  • 5,067
  • 2
  • 25
  • 57