20031109: Updated for Cocoon CVS 2.1.3 (8 nov 2003) and for Hibernate 2.1b6
20031113: Updated the attachment. Removed the classes already supplied by Woody.
- Read UsingHibernateToMakeYourJavaBeansPersistent if the instructions below go too fast.
- Add the Hibernate jars from the Hibernate distribution to
WEB-INF/lib. Copy only the jars that are not yet present. And don't forget the Hibernate jar itself (it is not in the Hibernate lib directory)
- Copy the content of
WEB-INFof the zip file (see attachment) into your Cocoon
- Merge the line in
_cocoon.xconfinto your real
cocoon.xconf(this adds the Avalon component that creates the Hibernate session)
WEB-INF/classes. Insert your login credentials for your database
- Create tables in your database with the
mysql.sqlscript. (Hibernate has support for many different databases. Change the SQL dialect in
hibernate.propertiesand edit this script if don't use mysql but something else.
- Copy the content (the woody map in the zip file) somewhere in your cocoon directory.
- Restart Tomcat. Near the end of the debug info in the console, there should be a line
Hibernate initialize called. If you don't see this line, one of the points above went wrong.
This is how it works:
- The Woody Java beans Contact, Sex, and Form2Bean are not modified.
- The sitemap of the Woody example is not modified.
- Two classes are added (The interface of an Avalon component
PersistenceFactoryand its implementation
HibernateFactory). This component creates the Hibernate session.
- The flowscript
binding_example.jsis changed a little. The function
form2beantries to load the
Form2Beanobject from disk with a hard-coded email address as the key. If it fails, it creates a new instance and saves it to disk.
- The real beef is in the file
WEB-INF\classes\org\apache\cocoon\woody\samples\Form2Bean.hbm.xml. This configuration file tells Hibernate how to do the real work: hide all details about the one-to-many relation (contacts that are added to the bean) for the developer. When you play with the example and look into the database, you can see that contacts are added with an unique key that is generated by Hibernate (the id field; don't edit it, as the Woody intructions tell you) and a foreign key (the email address). In a real-world application, you almost always will need the
inverseattributes. But in this case, I wanted to stay as close as possible to the original classes of the Woody sample.