Date: 2004-11-29T02:45:13
   Editor: JohannesTextor <[EMAIL PROTECTED]>
   Wiki: Cocoon Wiki
   Page: CocoonAndHibernateTutorial
   URL: http://wiki.apache.org/cocoon/CocoonAndHibernateTutorial

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -19,8 +19,14 @@
 
 === Your basic skills ===
 
+Time. If you are new to the topic of O/R mapping, you will need a lot 
+of time and patience, because it is not an easy topic. Learning Cocoon already
+made you re-think your concept of a Web Application; learning Hibernate, you
+will have to do it again. 
+
 You should have basic Cocoon knowledge, i.e. about the Sitemap, Flowscript,
-and JX Templates. The samples included will also use CForms. 
+and JX Templates. The samples included will also use CForms. Did I mention 
+Flowscript ? That one is especially important. 
 
 If you want to use Hibernate you should also be fluent in Java. Be warned that
 you are going beyond the "no programming required" - statement of the Cocoon 
@@ -29,6 +35,10 @@
 takes care of Hibernate Session, so if you don't know what a Servlet Filter is 
 get your favourite book or search engine and read up :) 
 
+You will have a basic notion of what Hibernate is and what it does, otherwise
+you would not have come to this page :) But make sure you have understood what
+lazy collection initialization is and what it does. 
+
 === Technical prerequisites ===
 
 A running cocoon installation. I tried this on Cocoon 2.1.6. It might work 
@@ -270,11 +280,47 @@
 or later experience the (in)famous Lazy Initialization Problem (tm). The 
solution for this problem is to
 create a servlet filter to manage Hibernate Sessions, so if you are'nt fed up 
on Java yet read on :) 
 
-= A Servlet Filter for Managing Hibernate Sessions =
+== A Servlet Filter for Managing Hibernate Sessions ==
+
+=== Why ? ===
+
+(If you just want to continue installing and are not bothering about the Why, 
skip this part and come
+back later ...) 
+
+If you're getting serious about Hibernate, sooner or later you will want to 
use lazy collection initialization
+(Read the corresponding section in GettingStartedWithCocoonAndHibernate for an 
introduction on this topic).
+Say you are accessing Hibernate sessions from flowscript, i.e. as follows: 
+
+{{{
+  var factory = cocoon.getComponent(Packages.org.test.PersistenceFactory.ROLE);
+  var hs = factory.createSession();
+
+  var data = hs.find("from org.test.Data");
+
+  cocoon.sendPage("data-pipe", {data: data} ); 
+
+  hs.close();
+}}}
 
-== Why ? ==
+This will work for simple data objects and also for complex ones, i.e. objects 
that contain collections, as long
+as you are not lazily initializing them. If not, you might experience the 
following problem: Say your object contains
+a mapped collection {{{otherData}}}. In the flow snippet above, you did not 
access this collection, so Hibernate
+won't load its items from the database. But it is still possible that your 
view pipeline wants to access {{{otherData}}}.
+
+Now the problem is that the flowscript will just continue to process right 
after it has invoked the view pipeline,
+so it is possible that the {{{hs.close()}}} will occur BEFORE your view has 
rendered completely. So when the view 
+will access {{{otherData}}} after that, you will get an exception telling you 
that Hibernate failed to lazily 
+initialize the collection. A classical Race Condition. 
+
+Obviously that's no good. The solution is to move Hibernate session management 
out of the flowscript layer and even 
+outside cocoon. So we will create a servlet filter. A servlet filer is invoked 
BEFORE the request is passed to cocoon 
+and will be notified when the request is processed completely. At that point 
we can safely close the Hibernate session,
+given we will not continue to work with it from flowscript after the view 
pipeline has been invoked. However, invoking
+the view pipeline should be the last thing you do in a flow script anyway, so 
that won't be too much of a problem.
 
+=== Creating the filter ===
 
+=== Installing the filter ===
 
 == Appendix ==