java - hibernate proxy object after system.out -
i'm new hibernate , have problem.
if this:
session sesion = hibernateutil.getsessionfactory().opensession(); transaction tx = sesion.begintransaction(); obj = (a) session.load(a.class,id); system.out.println(obj); tx.commit(); session.close(); return obj;
there no problem , gui shows object's data.
but if this:
session sesion = hibernateutil.getsessionfactory().opensession(); transaction tx = sesion.begintransaction(); obj = (a) session.load(a.class,id); // don't use system.out.println(obj); tx.commit(); session.close(); return obj;
the gui doesn't show , got following exception.
org.hibernate.lazyinitializationexception: not initialize proxy - no session
i've been reading api it's whole new world me. knows what's going on?
instead of using session.load(..)
, need use session.get(..)
a obj = (a) session.get(a.class,id);
the session.load(..)
lazy loads object using proxy, hence if object not accessed (in example using system.out.println
) object stays uninitialised. when uninitialised object accessed outside hibernate session (called detached object), lazyinitializationexception
generated - proxy object has no way of retrieving properties of object hibernate session.
the session.get(..)
doesn't lazy load object, still accessible outside session. there few other nuances of get
vs load
, thus, highly recommend visit following post difference:
understanding vs load: http://razshahriar.com/2009/01/hibernate-load-vs-get-differences/
Comments
Post a Comment