You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@cocoon.apache.org by Apache Wiki <wi...@apache.org> on 2005/08/10 09:05:37 UTC

[Cocoon Wiki] Update of "CocoonAndHibernateTutorial" by JohannesTextor

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cocoon Wiki" for change notification.

The following page has been changed by JohannesTextor:
http://wiki.apache.org/cocoon/CocoonAndHibernateTutorial

------------------------------------------------------------------------------
  - DOCUMENT-STATUS: '''*draft*''' reviewed released[[BR]]
  ----
  
- === Recent changes (as of 2005/07/25) ===
+ === Recent changes ===
+ 
+ 2005/08/10:
+ 
+ Finally added a minimal example to show how Cocoon and Hibernate interact. 
+ 
+ 2005/07/25: 
  
  If you have visited this page before, you should check out the new Servlet Filter. It now
  finally uses the request object instead of the servlet session to communicate with cocoon,
@@ -539, +545 @@

  Note: 
  Of course now you need to make sure that you manage a session (I.E. add the session action to your pipeline). If you will not add the session management, the filter will crash with "Cannot create a session after the response has been committed".
  
+ === A first Example === 
+ 
+ "Now that Hibernate is installed, how do I continue?" - Well the answer is up to you, since there are so many ways of using Hibernate. What follows is some kind of a minimal example so you get a feeling how Hibernate '''could''' interact with Cocoon. 
+ 
+ In this example we'll use Hibernate to manage a list of ISO language codes. So the first thing we need is a Java Bean that represents those language codes. Our bean will just have two attributes, the code and a name for each language, plus a numerical ID (I use one for each and every class). The code doesn't look all that interesting: 
+ 
+ {{{
+ package org.test;
+ 
+ public class DCLanguage {
+ 	private long ID;
+ 	private String IsoCode;
+ 	private String Name;
+ 	/**
+ 	 * 
+ 	 */
+ 	public DCLanguage() {
+ 		super();
+ 	}
+ 	/**
+ 	 * @return Returns the iD.
+ 	 */
+ 	public long getID() {
+ 		return ID;
+ 	}
+ 	/**
+ 	 * @param id The iD to set.
+ 	 */
+ 	public void setID(long id) {
+ 		ID = id;
+ 	}
+ 	/**
+ 	 * @return Returns the isoCode.
+ 	 */
+ 	public String getIsoCode() {
+ 		return IsoCode;
+ 	}
+ 	/**
+ 	 * @param isoCode The isoCode to set.
+ 	 */
+ 	public void setIsoCode(String isoCode) {
+ 		IsoCode = isoCode;
+ 	}
+ 	/**
+ 	 * @return Returns the name.
+ 	 */
+ 	public String getName() {
+ 		return Name;
+ 	}
+ 
+         /**
+ 	 * @param name The name to set.
+ 	 */
+ 	public void setName(String name) {
+ 		Name = name;
+ 	}
+ }
+ }}}
+ 
+ Compile this class and store it in the corresponding subfolder of WEB-INF/classes (Package name ...). 
+ Next, we shall create a table in our database to store the objects in. 
+ 
+ {{{
+ CREATE TABLE `dclanguage` (
+   `ID` int(11) NOT NULL auto_increment,
+   `IsoCode` varchar(255) NOT NULL default '',
+   `Name` varchar(255) NOT NULL default '',
+   PRIMARY KEY  (`ID`)
+ )
+ }}}
+ 
+ Fill in some sample values, or use the following if you're too lazy: :) 
+ 
+ {{{
+  INSERT INTO `dclanguage` VALUES (1,'de','german');
+  INSERT INTO `dclanguage` VALUES (2,'en','english');
+  INSERT INTO `dclanguage` VALUES (3,'fr','french');
+  INSERT INTO `dclanguage` VALUES (4,'ru','russian');
+  INSERT INTO `dclanguage` VALUES (5,'pl','polish');
+  INSERT INTO `dclanguage` VALUES (6,'da','danish');
+ }}}
+ 
+ The glue between our POJO and the SQL table is the hibernate mapping definition. This is an XML file which should be called {{{DCLanguage.hbm.xml}}}, since our class is called {{{DCLanguage.class}}}. Create the file with the following content and put it into the subfolder of WEB-INF/classes where your DCLanguage class resides: 
+ 
+ {{{
+ <?xml version="1.0"?>
+ <!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
+ "hibernate-mapping-2.0.dtd">
+ <hibernate-mapping>
+         <class name="org.test.DCLanguage" table="dclanguage">
+                 <id name="ID" column="ID" type="long">
+                         <generator class="native"/>
+                 </id>
+                 <property name="IsoCode" column="IsoCode" type="string"/>
+                 <property name="Name" column="Name" type="string"/>
+         </class>
+ </hibernate-mapping>
+ }}}
+ 
+ Attention, there is a catch: The {{{<generator class="native"/>}}} only works if your database is able to generate IDs automatically. (MySQl, for example, is). Refer to the Hibernate documentation if that does not hold in your case (HSQL?). 
+ 
+ Note that Hibernate comes with some tools which can automatically generate Java and SQL source code from a mapping file. They only work up to a certain level of complexity, though. 
+ 
+ Now you're almost done. Just insert the following statement: 
+ 
+ {{{cfg.addClass(DCLanguage.class);}}}
+ 
+ into the {{{HibernateFactory}}} (see ''ADD PERSISTENT CLASSES, VARIANT 1'' in the source code), recompile it, overwrite it in WEB-INF/classes and restart Cocoon. Congratulations, you should now be able to manage your ISO language codes via Hibernate and Cocoon :) 
+ 
+ To verify this, create a testing sub-sitemap, attach a flowscript and insert the following function into it: 
+ 
+ {{{
+ function language_list()
+ {
+ 	openHibernateSession(); // as defined in Tutorial 
+ 
+ 	var languages = hs.find("from org.test.DCLanguage order by IsoCode asc");
+ 		// HQL String to get a list of all beans, ordered by their IsoCode
+ 	
+ 	cocoon.sendPage("language-list.jxt", { 
+ 		languages:languages
+ 	}); // Send objects to the view layer (here: JX Template) 
+ 
+ 	// Hibernate Session will be closed automatically 
+ 	// by the servlet filter 
+ }
+ }}}
+ 
+ Create the corresponding pipelines in your sitemap: 
+ 
+ {{{
+ <map:match pattern="language-list.jxt">
+ 	<map:generate type="jxt" src="documents/language-list.jxt"/>
+ 	<map:serialize type="xml"/>
+ </map:match>
+ 
+ <map:match pattern="language-list">
+ 	<map:call function="language_list"/>
+ </map:match>
+ }}}
+ 
+ And save the following as {{{documents/language-list.jxt}}} (relative to your sitemap): 
+ 
+ {{{
+ <?xml version="1.0" encoding="iso-8859-1"?>
+ <languages xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+ 	 <jx:forEach var="lang" items="${languages}">
+ 		<language name="${lang.getName()}" isocode="${lang.getIsoCode()}" />
+ 	</jx:forEach>
+ </languages>
+ }}}
+ 
+ Alas, you're done. Surf to "base-uri-of-your-sitemap"/language-list and contemplate the power of Cocoon and Hibernate :) 
+ 
+ As said before, this is a minimal example. Now you're ready to continue on your own, the Hibernate docs will be your friend. Note that this example does not represent the recommended way of doing things - particularly since your querying your objects from Flowscript. Many people will rather want to do this from a Data Access Object (DAO) written in Java. The next part of this tutorial will cover this. 
+ 
  == Appendix ==
  
  === Links to other information sources ===