Hi all,
i have a problem working with lazy initiallization on collections.
I tried to use a proxy in my mapping file (same class) and
i tried to work with Hibernate.initialize(); at different places on my java-files.
Could someone tell me where change my source-code or mapping to get the lazy-thing working?
here my files i'm working with in a struts-web-environment.
Action_detail_isb_view.java
Code:
public class Action_detail_isb_view extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException, HibernateException {
FormBean_detail_isb insertForm = (FormBean_detail_isb) form;
Integer idUpdateIsb = new Integer(insertForm.getIdUpdateIsb());
Bean_Update_ISB detail_isb =
HibernateQueryUtil.loadUpdate_isb(idUpdateIsb);
...
Integer changeRequest = detail_isb.getChangeRequest();
Integer testTrack = detail_isb.getTestTrack_Nr();
Set List_Update = detail_isb.getList_Update();
...
request.setAttribute("listeUpdate", List_Update);
return (mapping.findForward("success"));
}
}
HibernateUtil.java --> from the official docsCode:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory =
new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(
"Exception building SessionFactory: " + ex.getMessage(),
ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
HibernateQueryUtil --> my own helper-class Code:
public static Bean_Update_ISB loadUpdate_isb(Integer id)
throws IOException, ServletException, HibernateException {
Session hibernate_session = HibernateUtil.currentSession();
Transaction tx = hibernate_session.beginTransaction();
Bean_Update_ISB update_isb = new Bean_Update_ISB();
try {
tx = hibernate_session.beginTransaction();
update_isb =
(Bean_Update_ISB) hibernate_session.load(
Bean_Update_ISB.class,
id);
tx.commit();
} catch (Exception e) {
e.printStackTrace(System.err);
if (tx != null)
tx.rollback();
}
finally {
HibernateUtil.closeSession();
}
return update_isb;
}
finally my mapping-file Code:
<hibernate-mapping>
<class name="com.tecdoc.db_update.Bean_Update_ISB"
table="t_Update_ISB">
<id name="id_Update_ISB" column="id_Update_ISB" type="integer">
<generator class="identity">
</generator>
</id>
<property name="Datum"
column="Datum"
type="java.lang.String"/>
<property name="Bezeichnung"
column="Bezeichnung"
type="java.lang.String"/>
<property name="Pfadangabe_Speicherort"
column="Pfadangabe_Speicherort"
type="java.lang.String"/>
<property name="ChangeRequest"
column="ChangeRequest"
type="integer"/>
<property name="TestTrack_Nr"
column="TestTrack_Nr"
type="integer"/>
<set name="List_Update" inverse="true" lazy="true">
<key column="id_Update_ISB"/>
<one-to-many class="com.tecdoc.db_update.Bean_Update"/>
</set>
</class>
</hibernate-mapping>
I always get
net.sf.hibernate.LazyInitializationException:
Failed to lazily initialize a collection - no session or session was closed
Thanks in advance.