微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用 size() 方法加载集合时如何解决 LazyInitializationException?

如何解决尝试使用 size() 方法加载集合时如何解决 LazyInitializationException?

环境:

  • Jboss 7.2
  • Java 11

CourseService(EJB) 使用 findById() 方法通过 Id 查找课程,然后尝试使用 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;
    }
...

课程豆

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

解决方法

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

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

    return Course;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。