【问题标题】:How to solve LazyInitializationException when you try to load collection with size() method?尝试使用 size() 方法加载集合时如何解决 LazyInitializationException?
【发布时间】:2021-02-02 11:41:41
【问题描述】:

环境:

  • Jboss 7.2
  • Java 11

CourseService(EJB) 通过 Id 使用 findById() 方法找到课程,然后尝试使用 size() 函数延迟加载 courseModuls,但我得到 LazyInizialitzationException。 这是从 Jboss 5.2 迁移而来的,它工作正常,但使用 Jboss7.2 时出现此错误。

知道那是什么吗?

课程服务

@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);
        }
    }
...

错误

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 org.hibernate@5.3.7.Final-redhat-00001//org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
    at org.hibernate@5.3.7.Final-redhat-00001//org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
    at org.hibernate@5.3.7.Final-redhat-00001//org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:160)
    at org.hibernate@5.3.7.Final-redhat-00001//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)

课程

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

【问题讨论】:

  • 因为findById()方法返回Course时事务结束
  • 尝试在 findByIdPartial() 方法上使用@Transactional 注解
  • @arch2be 那是,jboss7.2 似乎改变了行为,需要在方法中添加注解 Transacctional

标签: java hibernate lazy-initialization


【解决方案1】:

您应该在方法findByIdPartials() 中添加注释@Transactional,因为当方法findById() 返回Course 时,事务也结束。 在那里您可以找到有关此注释的更多信息:link 正确代码如下:

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

    return Course;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2018-02-10
    相关资源
    最近更新 更多