Hibernate Lazy Initialization

zhaozj2021-02-16  95

In a recent web project, use Hibernate as ORM, two tables and player to operate are just one-portrative relationship. I added a DAO between the business layer and the persistence layer, and the PO was obtained here, and then passed to the representation layer. I get a Team and then get Players through Team.getPlayers () at the representation layer, this time error. It is roughly that there is no session or session already closed. Later, checked Hibernate's document, it seems that I set lazy = true in the collection, so the collection is only loaded when reading, and the session is closed in DAO. The solution is not to use the delay initialization collection, or the statement to read the collection data is submitted before the SESSION is submitted. But there is no use of Lazy Initialization, which is not high. In the documentation, in the web application, you can use the servlet filter to turn off the session before the user request is completed, and then use a ThreadLocal variable to save the current session. Specific operations can go to Forum.javaeye.com.

转载请注明原文地址:https://www.9cbs.com/read-13995.html

New Post(0)