Hibernate & Design Patterns A Practical Overview

Post on 25-Feb-2016

37 views 0 download

Tags:

description

Hibernate & Design Patterns A Practical Overview. BSc . Thiago Oliveira Technical Manager Galileo Team Coimbra, April 14 th , 2009. Agenda. Design Patterns Transfer Objects Data Transfer Objects Factories Hibernate Framework Connecting the pieces. PART I Design Patterns & Hibernate. - PowerPoint PPT Presentation

Transcript of Hibernate & Design Patterns A Practical Overview

Powered by

Planner

Hibernate & Design PatternsA Practical Overview

BSc. Thiago OliveiraTechnical ManagerGalileo TeamCoimbra, April 14th, 2009

Powered by 2

Agenda

•Design Patterns

–Transfer Objects

–Data Transfer Objects

–Factories

•Hibernate Framework

•Connecting the pieces

PART IDesign Patterns & Hibernate

Powered by 4

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

Powered by 5

Transfer Objects

•Application need to exchange data with Beans/Persistence Layer trough a networkContext

•The Client invoke multiples times a get method to retrieve all necessary data•Such approach generates a network overheadProblem•Use a Transfer Object (TO) to encapsulate all the Business dataSolution

•Simplifies Entity Beans and Remote Interfaces•Transfer more Data in Fewer Remotes Calls•Reduces Network Traffic …

Consequences

Powered by 6

Transfer Objects

• A Transfer Object is just

a Serializable POJO

(Plain Old Java Object)

• Also called Persistent

POJO or Valuable Object

Powered by 7

Indentify Class “Version”.

Calculated Hash using the attributes

names and methods

signatures. It’s not a random number!

Powered by 8

Updatable Transfer Object Strategy

Powered by 9

Data Access Objects

•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 codeProblem

•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 dataSolution•Enables Transparency•Enables Easier Migration•Reduces Code Complexity in the Business Objects …

Consequences

Powered by 10

Data Access Objects

Powered by 11

Data Access Objects

•Implement the CRUD operations:

–public int insertResource(…)

–public boolean deleteResource(…)

–public ResourceTO findResource(…)

–public boolean updateResource(…)

Powered by 12

DAO – We want more flexibility

Concrete Factory

DAO – Implements the operation for a specific

Entity

Abstract Factory

Powered by 13

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

Powered by 14

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 )

Powered by 15

Hibernate Framework

• It provides:

– Transparent Persistence

– SQL generation

– Support for Transaction

Powered by 16

How this magic became true?

Through XML Configuration Files and

powerful API

–Hibernate Conf File (hibernate.cfg.xml)

–Mapping Files (myClass.hbm.xml)

Powered by 17

A connection configuration filehibernate.cfg.xml

Powered by 18

Powered by 19

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

•The actual stable

release is the JBoss Tools

3.0.0.GA, requires Eclipse

Ganymede 3.4.2

Powered by 20

Hibernate Tools for Hibernate 3.0

•Key Features

–Code Generation

–Wizards for creation of common Hibernate

files

–Support auto-completion and syntax

highlighting

Powered by 21

PART IIConnecting the Pieces

Powered by 23

Just Remembering

Powered by 24

A Simple ER

Powered by 25

A Employee Hibernate Mapping FileEmployeeTO.hbm.xml

Powered by 26

Yes, I Know… You want to see code…DepartmentDAO - Interface

Powered by 27

Yes, I Know… You want to see code…EmployeeDAO - Interface

Powered by 28

Yes, I Know… You want to see code…HbmDepartmentDAO – A concrete DAO

Powered by 29

Powered by 30

Yes, I Know… You want to see code…HbmDAOFactory – A Concrete Factory

Powered by 31

Powered by 32

Yes, I Know… You want to see code…DAOFactory – An Abstract Factory

Powered by 33

So we got a workspace structure like this…

Powered by 34

Ok, That’s beautiful… How do I use it?

Powered by 35

A deeper look attestInsertDepartment()

Powered by 36

A deeper look attestFindDepartmentById()

Powered by 37

A deeper look attestListDepartments()

Powered by 38

A deeper look attestFindDepartmentByName()

Powered by 39

A deeper look attestUpdateDepartment()

Powered by 40

A deeper look attestDeleteDepartment()

Powered by 41

Further Reading

•More about Hibernate & HSQL

–Hibernate Reference Document (

http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.p

df )

–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.htm

l )

•More about Transfer Objects

–Core J2EE Patterns - Transfer Objects (

http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html )

Powered by 42

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

)

http://www.assembla.com/wiki/show/gps/

BSc. Thiago OliveiraTechnical Manager

thiagosilvaoliveira@gmail.com

43