Using Hibernate with plain jWic
From JWic
Hibernate is maybe the mostly used open source O/R mapper available. This document explains how you can use this tool within your jWic application. Basic knowlege about Hibernate is recommended for this little tutorial.
Contents |
Prerequisites
You need to download the sample application code and extract it to a temporary directory.
Setup the WebApp
I assume that you have already setup a web application project that enables you to write jWic applications. Another required step is to add the main hibernate library and it's dependenies to the WEB-INF/lib path. You may also have to add the hibernate.jar file to your projects class path, before you can start coding.
Hibernate Configuration
Hibernate offers quit a lot of ways to configure it properly. For this tutorial, I have placed a hibernate.cfg.xml file into the classpath. This file contains the basic connection informations and a reference to the mapping files.
Sample:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="de/jwic/samples/hibernate/base/news.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Setup the Database
The sample configuration requires a MySQL server running on your localhost. If you are using a different database server, just change the configuration. We also need to create a database for our tests, that is named 'test' in the sample configuration. Create the database and run the following SQL scripts to create the required tables and fill them with some content:
DROP TABLE IF EXISTS `test`.`hibernate_unique_key`; CREATE TABLE `test`.`hibernate_unique_key` ( `next_hi` int(11) default NULL ); DROP TABLE IF EXISTS `test`.`news`; CREATE TABLE `test`.`news` ( `MESSAGE` text NOT NULL, `ID` int(10) unsigned NOT NULL auto_increment, `PARENT_ID` int(10) unsigned default NULL, PRIMARY KEY (`ID`) ); INSERT INTO test.news SET MESSAGE="Testmessage #1", ID=1; INSERT INTO test.news SET MESSAGE="Testmessage #2", ID=2, PARENT_ID=1; INSERT INTO test.news SET MESSAGE="Testmessage #3", ID=3, PARENT_ID=2;
Setup the application
- Copy the source files from the archive mentioned above into your projects source path.
- Open the WEB-INF/web.xml file and add the SessionUtil servlet interceptor to the configuration:
<web-app>
<servlet>
<servlet-name>jwic</servlet-name>
<servlet-class>de.jwic.web.DispatcherServlet</servlet-class>
...
<init-param>
<param-name>servlet.interceptors</param-name>
<param-value>de.jwic.samples.hibernate.util.SessionUtil</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
- Create a hibernate.xwic file to be able to start the application: (Or copy it from the archive)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE application SYSTEM "http://www.jwic.de/dtd/xwic-3.0.dtd"> <application> <name>HibernateDemo</name> <class>de.jwic.samples.hibernate.app.ViewEntitiesApplication</class> <singlesession>true</singlesession> <requireauth>false</requireauth> </application>
Start your Application
Now you should be able to start your application and study the sample.
Notice: I rather quickly implemented this sample and made it available due to user requests. I will keep on working on this sample to add some more possibilities (i.e. show DAO support), so it will be worth to check it out again in the near future...

