休眠:懒惰初始化异常:未能懒惰地初始化角色集合。无法初始化代理 - 无会话
2022-09-04 20:35:24
我有下一个错误: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());
...
}
}
我从另一个服务方法调用服务:
@Override
@JmsListener(destination = "listener")
public void handle(final Message message) throws Exception {
Model model = modelService.getById(message.getModelId());
serviceImpl.process(model);
modelService.update(model);
}
但是,当我尝试调用此方法时,我在行上也遇到异常,当我调用 .我已经检查了事务管理器,它已正确配置,并且此步骤中存在事务。此外,我已经检查了与此异常相关的stackoverflow上的大量答案,但没有任何用处。entities.add(entity);
getEntities()
model
可能是什么原因造成的?