Hibernate session load vs. get gotcha
The Hibernate documentation (3.2) for the session.get() and session.load() methods state:
load Returns: the persistent instance or proxy
get Returns: a persistent instance or nul
Notice “or proxy” in the load documentation. This last part took me an hour to figure out while debugging an application. In my case, for whatever reason (which I haven’t figured out), the load method was returning a proxy instead of an full instance, resulting in a state exception (closed session). For some unknown reason, this was only happening while running inside the web container and access the method through a transactional service. Test cases ran fine.
So changing my dao method from:
return (User) getSession().load(User.class, id)
to
return (User) getSession().get(User.class, id);
fixed everything.
| Print article |