Step By Step Guideline
for Building & Running
“HelloWorld” 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
[Link] the database
[Link] database schema (optional)
[Link] and populate database tables
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 [Link] under c:\hsqldb\sampledb
with the following two lines
● [Link].0=file:/hsqldb/sampledb/
● [Link].0=sampledb
– Run the database server at the command line
● java -classpath ..\lib\[Link] [Link] 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
[Link] domain classes (as POJO classes)
➢ [Link]
[Link] or generate Hibernate mapping files for
the domain classes
➢ [Link]
[Link] Hibernate configuration file (per
application)
➢ [Link]
11
Step 1: Write Domain Classes ([Link])
public class Person implements Serializable {
private int id;
private String name;
protected Person() {
}
public Person(int id, String name) {
[Link] = id;
[Link] = name;
}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
// Continued to next page
12
Step 1: Write Domain Classes ([Link])
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
13
Step 2: Write Mapping Files for Domain
Classes ([Link])
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"[Link]
<hibernate-mapping>
<class name="Person" table="person">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name" column="cname" type="string"/>
</class>
</hibernate-mapping>
14
Step 3: Write Hibernate configuration
file ([Link]) – Derby
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"[Link]
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">[Link]</proper
<property
name="[Link]">jdbc:derby://localhost:1527/mydatabase</property>
<property name="[Link]">app</property>
<property name="[Link]">app</property>
<!-- SQL dialect -->
<property name="dialect">[Link]</proper
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="[Link]"/>
</session-factory> 15
Step 3: Write Hibernate configuration
file ([Link]) – MySQL
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"[Link]
<hibernate-configuration>
<session-factory>
<property
name="connection.driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/test</property>
<property name="[Link]"></property>
<property name="[Link]"></property>
<property name="show_sql">true</property>
<property
name="dialect">[Link]</property>
<property name="[Link]">mysql</property>
<mapping resource="[Link]" />
</session-factory>
</hibernate-configuration>
16
Steps for Building and
Running the
Application
17
Steps to follow for Building and
Running the application
[Link] Hibernate library files to the classpath
[Link] the client JDBC client side driver to the
classpath
[Link] and run the application performing
database operation
18
Step 1: Copy Hibernate Library
Files to the classpath
● Hibernate library files from Hibernate distribution
– [Link]
– 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
– [Link] (for Derby)
– [Link] (for MySql)
– [Link] (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