Sometimes a LazyInitializationException will be thrown on getHibernateTemplate().load().
Reason being when using load(), Hibernate simply retrieves you a Proxy, which has only the id set, rest of the fields being queried from database when needed. At that time, the hibernate session might have been closed.
A quick fix: replace .load() with .get(). It'll fetch the result immediately.
Reason being when using load(), Hibernate simply retrieves you a Proxy, which has only the id set, rest of the fields being queried from database when needed. At that time, the hibernate session might have been closed.
A quick fix: replace .load() with .get(). It'll fetch the result immediately.
本文探讨了Hibernate中使用.get()代替.load()以避免LazyInitializationException的方法。.load()仅获取代理对象,其余字段按需从数据库加载,这可能导致Hibernate会话关闭时抛出异常。通过立即获取结果的.get()方法可以解决此问题。
826

被折叠的 条评论
为什么被折叠?



