Hibernate & Design Patterns - Assembla

flutheronioneyedSoftware and s/w Development

Dec 13, 2013 (3 years and 11 months ago)

74 views

Powered by
Planner
Hibernate & Design Patterns
A Practical Overview
BSc
. Thiago Oliveira
Technical Manager
Galileo Team
Coimbra, April 14
th
, 2009
Powered by
Agenda

Design Patterns

Transfer Objects

Data Transfer Objects

Factories

Hibernate Framework

Connecting the pieces
2
PART
I
Design Patterns & Hibernate
Powered by
What Are Design Patterns?

In the simplest term, it’s a solution to a
design problem in particular field

It’s define the problem, the solution, when
apply it and what are the consequences
4
Powered by
Transfer Objects

Application need to exchange data with Beans/Persistence Layer
trough a network
Context

The Client invoke multiples times a get method to retrieve all
necessary data

Such approach generates a network overhead
Problem

Use a Transfer Object (TO) to encapsulate all the Business data
Solution

Simplifies Entity Beans and Remote Interfaces

Transfer more Data in Fewer Remotes Calls

Reduces Network Traffic …
Consequences
5
Powered by
Transfer Objects

A Transfer Object is just
a Serializable POJO
(Plain Old Java Object)

Also called Persistent
POJO or Valuable Object
6
Powered by
ResourceTO
7
Indentify Class
“Version”.
Calculated Hash
using the attributes
names and
methods
signatures. It’s not
a random number!
Powered by
Updatable Transfer Object Strategy
8
Powered by
Data Access Objects
9

Access to data varies depending on the source of the data.
Depends on the kind of storage (databases, flat file, so on) and
vendor Implementation
Context

Potential creation of dependency between the application code
and the data access code
Problem

Use a Data Access Object (DAO) to abstract and encapsulate all
access to the data source. The DAO Manages the connection
with the data source to obtain and store the data
Solution

Enables Transparency

Enables Easier Migration

Reduces Code Complexity in the Business Objects …
Consequences
Powered by
Data Access Objects
10
Powered by
Data Access Objects

Implement the CRUD operations:

public
int
insertResource
(…)

public
boolean
deleteResource
(…)

public
ResourceTO
findResource
(…)

public
boolean
updateResource
(…)

11
Powered by
DAO

We want more flexibility
12
Concrete Factory
DAO

Implements the
operation for a specific
Entity
Abstract Factory
Powered by
Hibernate Framework

Hibernate is a object/relational mapping (ORM) tool for
Java

Hibernates goal is to relieve the developer from 95% of
common data persistence related programming task

Last Release: 3.3.1 GA
13
Powered by
Hibernate Framework

It provides:

Mapping from Java Objects to Relational Tables

Automatic conversion from Java Types to SQL Types

Data Query and retrieve facilities that can reduce the time
effort of development

Sophisticated Query Options you can use pure SQL or HQL
(Hibernate Query Language )
14
Powered by
Hibernate Framework

It provides:

Transparent Persistence

SQL generation

Support for Transaction
15
Powered by
How this magic became true?
Through XML Configuration Files and
powerful API

Hibernate Conf File (
hibernate.cfg.xml
)

Mapping Files
(
myClass.hbm.xml
)
16
Powered by
A connection configuration file
hibernate.cfg.xml
17
Powered by
18
Powered by
We don’t want to write any single
configuration file by hand…

JBoss
Tools is a set of
plugins
for Eclipse for
support
JBoss
and
related technology, like
Hibernate
19

The actual stable
release is the
JBoss
Tools
3.0.0.GA, requires Eclipse
Ganymede 3.4.2
Powered by
Hibernate Tools for Hibernate 3.0
20

Key Features

Code Generation

Wizards for creation of common
Hibernate files

Support auto
-
completion and syntax
highlighting
Powered by
21
PART
II
Connecting the Pieces
Powered by
Just Remembering
23
Powered by
A Simple ER
24
Powered by
A Employee Hibernate Mapping File
EmployeeTO.hbm.xml
25
Powered by
Yes, I Know… You want to see code…
DepartmentDAO
-
Interface
26
Powered by
Yes, I Know… You want to see code…
EmployeeDAO
-
Interface
27
Powered by
Yes, I Know… You want to see code…
HbmDepartmentDAO

A concrete DAO
28
Powered by
29
Powered by
Yes, I Know… You want to see code…
HbmDAOFactory

A Concrete Factory
30
Powered by
31
Powered by
Yes, I Know… You want to see code…
DAOFactory

An Abstract Factory
32
Powered by
So we got a workspace structure like
this…
33
Powered by
Ok, That’s beautiful… How do I use it?
34
Powered by
A deeper look at
testInsertDepartment
()
35
Powered by
A deeper look at
testFindDepartmentById
()
36
Powered by
A deeper look at
testListDepartments
()
37
Powered by
A deeper look at
testFindDepartmentByName
()
38
Powered by
A deeper look at
testUpdateDepartment
()
39
Powered by
A deeper look at
testDeleteDepartment
()
40
Powered by
Further Reading

More about Hibernate & HSQL

Hibernate Reference Document
(
http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.pdf
)

BAUER, Christian; KING, Gavin.
Java Persistence with Hibernate
. Manning
Publications Co: New York, 2007

More about Data Access Objects

Core J2EE Patterns
-
Data Access Objects
(
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
)

More about Transfer Objects

Core J2EE Patterns
-
Transfer Objects
(
http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
)
41
Powered by
Further Reading

More about
Junit

Unit testing with
JUnit
4.x and
EasyMock
in Eclipse
(
http://www.vogella.de/articles/JUnit/article.html
)

Junit
4.x How to
(
http://pub.admc.com/howtos/junit4x/junit4x.pdf
)

More about Hibernate Tools

Hibernate Tools Reference Guide
(
http://docs.jboss.org/tools/3.0.0.GA/en/hibernatetools/pdf/Hibernatetools_Reference
_Guide.pdf
)
42
http://www.assembla.com/wiki/show/gps/
BSc
. Thiago Oliveira
Technical Manager
thiagosilvaoliveira@gmail.com
43