Step by Step Guideline For Building Running Hello World Hibernate Application
Step by Step Guideline For Building Running Hello World Hibernate Application
1
What we are going to build
● A simple Hibernate application persisting
Person objects
● The database table, person, has the following
columns
– int id
– string cname
● We will use Derby, HSQL and MySQL as
databases
● We will add data to and retrieve data from the
table through Hibernate
2
Things to do
● Start database server and populate database
tables
● Write source files
● Build and run the applications
3
Steps for Starting the
database and
populating the tables
4
Steps to follow
5
Step 1: Start the database
● 3 Different options
– At the command line
– Through an IDE
– As windows service (Not all databases support this)
6
Step 1: Start the database server
● Derby within the IDE
– From NetBeans, select Tools->Java DB Database-
>Start Java DB Server
● HSQLDB at the command line
– Create a directory to host a database
● mkdir c:\hsqldb\sampledb
– Create server.properties under c:\hsqldb\sampledb
with the following two lines
● server.database.0=file:/hsqldb/sampledb/
● server.dbname.0=sampledb
– Run the database server at the command line
● java -classpath ..\lib\hsqldb.jar org.hsqldb.Server 7
Step 2: Create Database Schema
(Optional Step)
● 2 different options
– Manually
– Use SchemaExport utility tool that comes with
Hibernate
● You can generate the database schema from the mapping
files
● For this sample application, we will not create
the schema, instead we will create or populate
the database table either manually or
programmatically (within Java program)
8
Step 3: Create and Populate the
database tables
● 4 different options
– Running Schema (SQL commands) at the command
line
– Running SQL command within the IDE
– Create the table manually using database manager
tool or within the IDE
– From the Java program
9
Steps for Writing
Source Files
10
Steps to follow for Writing files
11
Step 1: Write Domain Classes (Person.java)
public class Person implements Serializable {
protected Person() {
}
13
Step 2: Write Mapping Files for Domain
Classes (Person.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Person" table="person">
<id name="id" type="int">
<generator class="increment"/>
</id>
<hibernate-configuration>
<session-factory>
<property
name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="show_sql">true</property>
<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">mysql</property>
18
Step 1: Copy Hibernate Library
Files to the classpath
● Hibernate library files from Hibernate distribution
– hibernate3.jar
– other dependency files
19
Step 2: Add the database driver to
the classpath
● The application as a database client needs to
have database-specific database driver
– derbyclient.jar (for Derby)
– mysql-connector-java-5.1.6-bin.jar (for MySql)
– hsqldb.jar (for HSQLDB)
20
Step 3: Compile and run the
application
● As a standalone application or Web application
● In this example, we will run it as a standalone
application (that has a main() method)
21
Questions?
22