![](/img/trans.png)
[英]Hibernate - failed to lazily initialize a collection of role: could not initialize proxy - no Session
[英]failed to lazily initialize a collection of role : could not initialize proxy - no Session
Exception:
failed to lazily initialize a collection of role: com.cerner.budgetmanagement.model.Team.users, could not initialize proxy - no Session (through reference chain: com.cerner.budgetmanagement.model.Team["users"])
有人可以解釋為什么會這樣嗎? 我學習在春季/冬日環境中使用運動衫。 我對這種延遲初始化的理解是因為會話超出范圍。 提前致謝。
@SuppressWarnings({ "unchecked"})
@GET
@Path("/name/{teamId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getBudgetPlan(@PathParam("teamId") int teamId) {
teamDao = applicationContext.getBean("dao", Dao.class);
final Team team = teamDao.getModelById(Team.class, teamId);
Hibernate.initialize(team);
return Response.status(201).entity(team).build();
}
我想您有一個惰性集合要為Team.users加載。 這可能與您的TransactionManager有關。
為您的HibernateSession創建一個TransactionManager(如果尚未創建),並使用@Transactional注釋getModelById方法。
您的問題是您試圖初始化一個懶惰的關系。 初始化意味着Hibernate嘗試執行SELECT
語句以獲取數據。
這是有問題的,因為您沒有正在初始化的代碼周圍的任何Transaction
。
請參閱我的文章以了解詳細信息: http : //blog.arnoldgalovics.com/2017/02/27/lazyinitializationexception-demystified/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.