休眠:LazyInitializationException:无法延迟初始化角色集合.无法初始化代理-没有会话 [英] Hibernate: LazyInitializationException: failed to lazily initialize a collection of role. Could not initialize proxy - no Session

查看:147
本文介绍了休眠:LazyInitializationException:无法延迟初始化角色集合.无法初始化代理-没有会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到下一个错误:nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.Model.entities, could not initialize proxy - no Session

我的Model实体:

class Model {
...
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "model", orphanRemoval = true)
    @Cascade(CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    public Set<Entity> getEntities() {
        return entities;
    }

    public void addEntity(Entity entity) {
        entity.setModel(this);
        entities.add(entity);
    }

}

我有一个服务班:

@Service
@Transactional
class ServiceImpl implements Service {
    @Override
    public void process(Model model) {
        ...
        model.addEntity(createEntity());
        ...
    }
}

我正在通过其他服务方法致电服务

I'm calling service from another service method:

@Override
@JmsListener(destination = "listener")
public void handle(final Message message) throws Exception {
    Model model = modelService.getById(message.getModelId());
    serviceImpl.process(model);
    modelService.update(model);
}

但是当我尝试调用此方法时,我在行entities.add(entity);上获取了异常,当我在model上调用getEntities()时,也会发生相同的异常. 我已经检查了事务管理器,它的配置正确,并且此步骤中存在事务.另外,我已经检查了关于与此异常相关的stackoverflow的大量答案,但是没有什么用.

But when I'm trying to call this method I'm getting exception on line entities.add(entity); also the same exception occurs when I'm calling getEntities() on model . I've checked transaction manager and it's configured correctly and transaction exists on this step. Also I've checked tons of answers on stackoverflow connected to this exception but nothing useful.

可能是什么原因?

推荐答案

看来模型是一个独立的实体.

It seems that model is a detached entity.

尝试合并并在合并实例上执行操作:

Try to merge and perform operations on a merge instance:

@Override
public void process(Model model) {
     ...
    Model mergedModel = session.merge(model);

    mergedModel.addEntity(createEntity());
    ...
}

这篇关于休眠:LazyInitializationException:无法延迟初始化角色集合.无法初始化代理-没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆