Professional Documents
Culture Documents
01-Hibernate-Introduction To Hibernate PDF
01-Hibernate-Introduction To Hibernate PDF
com
Introduction to
Hibernate
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/hibernate.html
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
2009 coreservlets.com
Courses developed
and taught
by EE
coreservlets.com
experts (edited by Marty)
Customized
Java
Training: http://courses.coreservlets.com/
Spring, Hibernate/JPA, EJB3, Ruby/Rails
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Contact
hall@coreservlets.com
for details
Developed and taught by well-known
author
and developer. At public
venues or onsite at your location.
Topics
p
in this Section
Refresher in enterprise application
architectures
hi
Traditional persistence
Hibernate
Hib
t motivation
ti ti
Installation
2009 coreservlets.com
Enterprise Application
Architectures
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
N-Tier Architecture
Application is made up of layers or tiers
Each layer encapsulates specific responsibilities
Enables changes in one area with minimal impact to other
areas of the application
Common tiers
Presentation
View in model-view-controller
Responsible for displaying data only. No business logic
Service
Responsible for business logic
Persistence
Responsible for storing/retrieving data
N-Tier Architecture
Presentation
DTO
Service
DTO
Persistence
JDBC
Database
DAO Design
g Pattern
Data Access Object
Abstracts CRUD (Create, Retrieve, Update, Delete)
operations
Benefits
Allows different storage implementations to be plugged
in with minimal impact
p to the rest of the system
y
Decouples persistence layer
Encourages and supports code reuse
Implementing
p
g Business Logic
g
Service Layer
Thin domain layer
Procedural service layer
Fowler Anemic
Anemic Domain Model
Model
Design
g Approaches
pp
[D1] Service layer contains all business
logic (no real domain model)
[D2] Complex OO domain model/thin
service layer
[D3] Service layer contains use case
logic that operates over thin or
ode ate y complex
co p e domain
do a model
ode
moderately
DTO
Service
DTO
Persistence
Database
[[D2]] Object
j
Oriented Approach
pp
Complex OO domain model/thin service layer
Rich object model utilizing standard design patterns,
delegation, inheritance, etc.
Distinct API to domain model
Need complex
p
Data Mapper/Data
pp
Store since
domain model and database schema are likely
different
TopLink,
T Li k JDO,
JDO Hibernate
Hib
t
[[D2]] Object
j
Oriented Approach
pp
Presentation
DO
S i
Service
DO
DO
DO
DO
Persistence
Database
U
Uses advantages
d
t
off b
both
th extremes
t
Difficult to remain consistent within the
same application
2009 coreservlets.com
Traditional
Persistence
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Traditional Persistence
Presentation
DTO
Service
DTO
Session Facade
Service
DTO
DTO
Persistence
JDBC
Persistence
Entity
B
Bean
Database
Database
17
APP SER
RVER
JDBC Overview
JDBC API provides ability to
Home Interface
Remote Interface
B
Bean
Class
Cl
(i
(implementation
l
t ti class)
l
)
ejb-jar.xml
Container specific
p
EJB descriptor
p ((<container>-ejb-jar.xml)
j j
)
Cach
he
Domain O
Objects
HIBER
RNATE
Presentation
Business
Persistence
Database
32
2009 coreservlets.com
M ti ti
Motivation
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Hibernate History
y
Grass roots development (2001)
Christian Bauer
Gavin King
NHibernate
.NET version release in 2005
Hibernate Goals
Prevent leakage of concerns
Domain model should only be concerned about modeling
the business process, not persistence, transaction
management and authorization
Flaw
Fl off EJB2.x
EJB2
Metadata in XML
Object/Relational Mapping should provide human
readable mapping format (not just a GUI mapping tool)
Reduction in LOC
OC
Importance of domain object model
Why
y Hibernate?
Impedance mismatch
Object-oriented
Obj
i
d vs. relational
l i l
Why
y not Hibernate?
AT&T
PriceWaterhouseCoopers
Cisco
Sony
38
Source hibernate.org
39
40
2009 coreservlets.com
I t ll ti
Installation
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Hibernate Step
p1
http://www.hibernate.org
Click
Cli k on D
Downloads
l d link
li k
Hibernate Step
p2
Select the core binary relase
Downloads
Hibernate Step
p3
Unzip the Download
hibernate-distribution-3.3.1.GA-dist.zip
hib
di ib i 3 3 1 GA di i
Copy jars from locations under root of zip
hibernate3.jar
hibernate-distribution-3.3.1.GA/lib/required
Drop the jars from into the lib directory of your project (or other
location you can add to your projects classpath)
Hibernate Step
p4
Within Eclipse
Oracle Express
p
Step
p1
http://www.oracle.com/technology/products/dat
abase/xe/index html
abase/xe/index.html
Download OracleXE.exe install
Also available for Linux
Download
Debian,
Debian Mandriva,
Mandriva Novell
Novell, Red Hat and Ubuntu
Oracle Express
p
Step
p2
Run OracleXE.exe
Oracle Express
p
Step
p3
Confirm
Complete
Starting
g and Stopping
pp g Oracle
Oracle is automatically running upon install
Start and stop Oracle using the StartDB and
StopDb scripts
C:\oracleexe\EE\app\oracle\product\10.2.0\server\BIN\StartDB.bat
C\
l
\EE\ \
l \ d t\10 2 0\
\BIN\St tDB b t
C:\oracleexe\EE\app\oracle\product\10.2.0\server\BIN\StopDb.bat
JavaDB Configuration
g
JavaDB is a version of Derby that comes
packaged
k
d with
i h Java
J
6
Configuration set environment variables
DERBY_HOME
DERBY HOME
Value should be location of JavaDB root directory
Example: C:\Program Files\Sun\JavaDB
PATH
Append JavaDB bin directory to existing PATH variable
Example: C:\Program Files\Sun\JavaDB\bin
JavaDB Configuration
g
Start Server by calling startNetworkServer
script
i
Stop Server
Ser er b
by calling stopNet
stopNetworkServer
orkSer er
script (in another window)
2009 coreservlets.com
Wrap-up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
53
Summary
y
Refresher in application architectures
Service-based
Service based business logic
Rich domain model
Combination
JDBC example
Motivation
Origination
g
and historyy of Hibernate
Reasons for Hibernates development
Impedance mismatch
Failure of EJB 2.x
Java developers are not database developers
Performance benefits
54
Installation
2009 coreservlets.com
Q
Questions?
ti
?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.