Java Hibernate: LazyInitializationException: 未能延迟初始化角色集合。无法初始化代理 - 没有会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42925823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: LazyInitializationException: failed to lazily initialize a collection of role. Could not initialize proxy - no Session
提问by Orest
I have next error: nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.Model.entities, could not initialize proxy - no Session
我有下一个错误: nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.Model.entities, could not initialize proxy - no Session
My Model
entity:
我的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);
}
}
And I have a service class:
我有一个服务类:
@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);
}
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.
但是,当我试图调用这个方法我收到线异常entities.add(entity);
,当我打电话时,也会发生同样的异常getEntities()
上model
。我已经检查了事务管理器,它配置正确,并且此步骤中存在事务。此外,我已经检查了与此异常相关的 stackoverflow 上的大量答案,但没有任何用处。
What could be the cause of it?
可能是什么原因造成的?
采纳答案by Maciej Kowalski
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());
...
}
回答by Orest
So as @Maciej Kowalski mentioned after first @Transactional
read of my model
it's already in deatached state and call to get entities
from another @Transactional
method failed with LazyInitializationException
.
因此,正如@Maciej Kowalski 在第一次@Transactional
阅读我的文章后提到的那样,model
它已经处于分离状态并且entities
从另一个@Transactional
方法中调用 get失败了LazyInitializationException
。
I've changed my service a bit to get model
from database in the same transaction:
我已经改变了我的服务,以便model
在同一个事务中从数据库中获取:
@Service
@Transactional
class ServiceImpl implements Service {
@Override
public void process(long modelId) {
...
Model model = modelDao.get(modelId);
model.addEntity(createEntity());
...
}
}
Now everything works as expected.
现在一切都按预期进行。