Introduction
Hibernate is a popular and powerful ORM(Object Relational Mapping) Framework. When you
dealing with the database programming, it can significantly speed up your
productivity and simplify the procedure of development. In this application I will show you how struts2 and hibernate3 work together and how simple it is to add hibernate to our struts2crud project.
Requirements
This application requires an application server that
implements the Servlet 2.4 and JavaServer Pages 2.0 specifications. The
example has been tested on Tomcat 5.x and Tomcat 6.0
Database Setup
We use MySQL5 as our database in this application. Be sure you've installed the correct version of MySQL and it is running before you create our example database.
Creating example database and loading sample data
Important: Remember that replaces to your own MySQL database username and password of the hibernate.cfg.xml file. You can find the hibernate.cfg.xml under \web-inf\classes after Tomcat unpack the war file. I highlight them below on the hibernate.cfg.xml. Unless you've done this,the hibernate won't work properly.
Creating example database and loading sample data
- download the source file of our application
- unzip into directory of choice
- find the xdb.sql file under a folder named db_build
- copy xdb.sql to your partation c:
- run your MySQL Command Line Client.
- after entering your password, you should see a prompt line "mysql>"
then input the following command and run it:
\. c:/xdb.sql
Important: Remember that replaces to your own MySQL database username and password of the hibernate.cfg.xml file. You can find the hibernate.cfg.xml under \web-inf\classes after Tomcat unpack the war file. I highlight them below on the hibernate.cfg.xml. Unless you've done this,the hibernate won't work properly.
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/xdb</property>
<property name="connection.username">Your User Name</property>
<property name="connection.password">Your Password</property>
<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<propertyname="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="net/learntechnology/struts2demo/vo/Department.hbm.xml"/>
<mapping resource="net/learntechnology/struts2demo/vo/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(Rest of lesson coming soon. For a sample Strut2 CRUD application without using hibernate see my Struts2 CRUD lesson.)