Orm Ababneh
Orm Ababneh
Ehab Ababneh
Outline
• Introduction
• Hibernate
▫ A first Example
▫ Hibernate’s Architecture
▫ Hibernate’s Main Classes
▫ Mapping entities’ relationships (i.e. entities to
objects)
▫ Mapping OO relationships (i.e. classes to entities)
Mapping associations
Mapping Containers
Mapping inheritance
Introduction
• Objects are the basic building blocks for software
systems.
• And, generally speaking, a software system need
to persist its state.
• Relational data model is the most prevalent
(Oracle, MySQL, SQLServer, etc.).
• Bridge the gap between different paradigms.
Introduction
• Not only that, but it also provides data query and
retrieval facilities and can significantly reduce
development time otherwise spent with manual
data handling in SQL and JDBC
• We will be looking at Hibernate as an example.
Hibernate
Hibernate
• Hibernate is free.
• Hibernate is an open source project.
• Hibernate currently is in release 3.6.8. But a
fifth CR of 4.0.0 is out as well.
• Founder and current project leader is Gavin
King.
Configuring Hibernate, Storing and retrieving objects
A First Example
• Persist an object
• Retrieve an object
The Setup
• The database: employees database from Launchpad.
▫ The database: http://launchpadlibrarian.net/24493586/
employees_db-full-1.0.6.tar.bz2
▫ Installation Instructions: http://dev.mysql.com/doc/employee/
en/employee.html#employees-installation
• Hibernate runtime, which can be downloaded from
SourceForge.
• JPA from "lib\jpa" within the hibernate distribution.
• Jar files in the "lib\required" folder in hibernate's
distribution.
• Self4j
• MysqlConnectorJ.
The Setup
• Schema
A First Example
• What do we need in order to persist and retrieve
an object?
A First Example
• A way to tell Hibernate about the database
• Our persistent class
• A way to tell Hibernate how to map the
persistent class to the database table(s)
• The main program to glue thing together
A First Example
1 <?xml version='1.0' encoding='utf-8'?>
• Hibernate Configuration 2 <!DOCTYPE hibernate-configuration PUBLIC
file 3 "-//Hibernate/Hibernate Configuration DTD//EN"
▫ File name is 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
"hibernate.cfg.xml" and the 5
file itself is in the class path. 6 <hibernate-configuration>
▫ Lines 8-10 specify that we 7 <session-factory>
are using a mysql JDBC
driver. So, it must be in the 8 <property name="hibernate.connection.driver_class">
class path. 9 com.mysql.jdbc.Driver
▫ Lines 11-13 specify the 10 </property>
connection URL to the 11 <property name="hibernate.connection.url">
database. The database name 12 jdbc:mysql://localhost/employees
is "employees" and mysql is 13 </property>
running on localhost on
default port. 14 <property name="hibernate.connection.username">root</property>
▫ Lines 14-15 are database user 15 <property name="hibernate.connection.password“></property>
account information. 16 <property name="hibernate.connection.pool_size">10</property>
▫ Line 18 tells hibernate to 17 <property name="show_sql">true</property>
user mysql's flavor of SQL or 18 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
"dialect". Other dialects are
listed later on. 19 <property name="hibernate.hbm2ddl.auto">update</property>
▫ Line 21 specifies a Class-to- 20 <!-- Mapping files -->
Table mapping file (shown 21 <mapping resource=“employee.hbm.xml"/>
later). 22
23 </session-factory>
24 </hibernate-configuration>
A First Example
• Persistence Class
1 package hibernatetutorial.entities;
▫ This is the java class that is to be 2
mapped to a database table. 3 import java.util.Date;
4
▫ The persistent class follows the 5 public class Employee {
JavaBean standard (no-arg 6
constructor in addition to a setter 7 String firstName;
8 String lastName;
and getter for each mapped
9 Date birthDate;
attribute). 10 Date hireDate;
11 char gender;
strategy:
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
strategy:
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">