---
title: "[Hibernate] one cause of LazyInitializationException"
author: "SilverRing"
date: "2010-10-19T16:08:36Z"
tags: []
categories: ["企业架构"]
slug: "hibernate-one-cause-of-lazyinitializationexception"
draft: false
lastmod: "2010-10-19T16:08:36Z"
id: 788660
url: "https://silverring.iteye.com/blog/788660"
---

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.