cocoon-docs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@cocoon.apache.org
Subject [Cocoon Wiki] Updated: GettingStartedWithCocoonAndHibernate
Date Tue, 30 Nov 2004 09:07:39 GMT
   Date: 2004-11-30T01:07:39
   Editor: JohannesTextor <jc.textor@gmx.de>
   Wiki: Cocoon Wiki
   Page: GettingStartedWithCocoonAndHibernate
   URL: http://wiki.apache.org/cocoon/GettingStartedWithCocoonAndHibernate

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -1,16 +1,93 @@
 = Why would you want to do this?  =
 
 ''brief outline of the benefits of using an object relational mapping tool, like for Hibernate
or OJB''
+
+The main benefit of an O/R mapping tool is that it enables you to deal with objects instead
of database tables.
+Suppose you are building a web application based on Cocoon alone. You have a database of
articles that is supposed
+to be navigated by users. The pure cocoon / flowscript MVC approach would be something like:

+
+{{{
+     // ***** Get data from SQL Database (Model Layer) ***** 
+
+     var db_conn = Database.getConnection("name");
+     var s = db_conn.createStatement();
+     var r = s.executeQuery("SELECT * FROM articles;");
+
+     var articles = new Array(); 
+     var ri = r.iterator(); 
+     while(article = ri.next()) 
+        articles[articles.length] = new Article( article.getString("Name"), article.getInt("Price"),
... ) )
+
+     // ***** Process data (Control Layer) ***** 
+
+     /* Do some processing, e.g. add images, comments etc. to the articles in the array or
using CForms */ 
+
+     // ***** Send data to view (View Layer) ***** 
+
+     cocoon.sendPage("article-view",{articles:articles});
+}}}
+
+We love flowscript, don't we? In the above example, you can see that both the Control and
the View layer are object oriented.
+However, the Model layer is a relational database, which makes conversion between M and C
necessary - the typical kind of 
+boring, repetitive and error-prone coding presented above. Now comes the same code fragment
using Hibernate: 
+
+{{{
+     // ***** Get data from Hibernate (Model Layer) ***** 
+
+     var hs = openHibernateSession();
+     var articles = hs.find("from org.test.Article"); 
+
+     // ***** Process data (Control Layer) ***** 
+
+     /* Do some processing, e.g. using CForms to modify some articles or using CForms  */

+
+     // ***** Send data to view (View Layer) ***** 
+
+     cocoon.sendPage("article-view",{articles:articles});
+}}}
+
+Well this obviously makes your life easier. A Hibernate Session can be thought of as the
object-oriented equivalent
+to a JDBC connection. As you can also see, Hibernate has its own - yet SQL-similar - query
language. The fundamental
+difference is that Hibernate queries yield '''objects''' instead of database rows, so there
is no further conversion
+necessary. You can directly process the result vector and pass it on to the View. 
+
+This is not only true for "flat" beans with simple attributes, it also holds for complex
beans with collection
+attributes, sets, maps, vectors etc - and that is the point where it would get really messy
to construct these 
+"by hand" from a relational DB using flowscript. That part is now done by Hibernate. 
+
+The actual mapping between POJO Objects and the database is done by Hibernate. To accomplish
this, it uses so-called
+mapping files. These can - up to a certain degree of complexity - also be automatically generated
from Java code using
+tools that come with Hibernate. Even if you create these files by hand, it is still easier
and more structured than
+writing endless flowscript code. 
+
+== Summary ==
+
+Hibernate makes the Model layer of an MVC application compatible with OO-based V and C layers.
A much higher degree of 
+abstraction from DB details is the result. 
+
+Again an outline of the main benefits: 
+
   *  You don't have to write endless SQL access code anymore. The O/R mapping tool generates
all necessary SQL access code behind the scenes and hands you the results in POJO format.
   *  You can manipulate your database (read, insert, delete) using POJO's and nothing but
POJO's.
   *  Hibernate comes with transparent connection pooling (DBCP) and query caching (EHCache),
only a matter of configuration.
 
 = What must you know before you start? =
 
+Be aware that you will not be ready to write Hibernate-based applications in 5 minutes. You
are about to venture into a complex 
+topic. Sit back, get a cup of tea and prepare for some days of reading and learning. Fortunately,
the Hibernate framework is 
+excellently documented (see http://www.hibernate.org/). The following skills are mandatory:

+
 ''Skills and depth of knowledge required''
   *  You need to be proficient in Java, have basic SQL knowledge in case you want to do troubleshooting.

-  *  You should now how to translate your application domain into a solid database model.
+  *  You should know how to translate your application domain into a solid database model.
   *  Understand the "lazy loading" concept that most O/R mapping tools use by default. This
generally means that nothing is retrieved from the database until you really ask for it. [http://www.hibernate.org/162.html].
Also see below for notes on lazy collection initialization. 
+
+'''Make sure''' that you have understood the last point - if not, read the Hibernate documentation
and then come 
+back here. Otherwise you will only get frustrated with the task of integrating Hibernate
into cocoon. 
+
+It must be made clear at this point that all documentation on this wiki focuses on the particular
issue of
+integrating Hibernate into cocoon, which is a challenging but rewarding task. Rewarding,
since you will learn 
+some things about Cocoon's internal structure you might not have known before. 
 
 ''Skills and depth of knowledge that could be useful''
 

Mime
View raw message