Search code examples
javahibernatelazy-initialization

How to solve LazyInitializationException when you try to load collection with size() method?


Environment:

  • Jboss 7.2
  • Java 11

The CourseService(EJB) find the course with findById() method by Id an then try to lazy loading courseModuls with size() function, but I get LazyInizialitzationException. This is a migration from Jboss 5.2 and it worked properly but with Jboss7.2 I get this error.

Any idea what is that?

CourseService

@Stateless
@Local
@RolesAllowed(RolConstants.EVERYONE)
public class CourseService extends BusinessService<Course> implements CourseServiceable {
...
    @Override
    @PermitAll
    public Course findByIdPartial(Object id) throws AppException {
        Course Course = findById(id);
        Course.getCourseModuls().size();

        return Course;
    }
...

CourseBean

@Named
@ViewScoped
public class CourseBean extends LazyBean<Course> implements Serializable {
...
    public void load() {
        try {
            selected = service.findByIdPartial(selected.getId());
        } catch (AppException e) {
            log.error("CourseBean.load()", e);
            faceMessage.error("error.load", e);
        }
    }
...

Error

12:15:19,160 SEVERE [org.primefaces.application.exceptionhandler.PrimeExceptionHandler] (default task-1) failed to lazily initialize a collection of role: es.caib.forma.business.form.entity.Course.courseModuls, could not initialize proxy - no Session: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: es.caib.forma.business.form.entity.Course.courseModuls, could not initialize proxy - no Session
    at [email protected]//org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
    at [email protected]//org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
    at [email protected]//org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:160)
    at [email protected]//org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:287)
    at deployment.fora2.ear//es.caib.fora.business.formacio.boundary.CourseService.findByIdPartial(CourseService.java:337)
    at deployment.form2.ear.forma-front.war//es.caib.forma.presentation.front.form.CourseBean.load(CourseBean.java:104)

Course

@Entity
@Table(name = "COURSE")
public class Course implements Serializable {
...
   @OneToMany(mappedBy = "curs", cascade = { CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH })
    private List<CursModul> cursModuls;
...

Solution

  • You should add annotation @Transactional to the method findByIdPartials(), because when method findById() return Course then transaction end too. There you can find more information about this annotation: link Correct code below:

    @Override
    @PermitAll
    @Transactional
    public Course findByIdPartial(Object id) throws AppException {
        Course Course = findById(id);
        Course.getCourseModuls().size();
    
        return Course;
    }