Hibernate & Design Patterns A Practical Overview

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

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

Page 1: Hibernate & Design Patterns A Practical Overview

Powered by

Planner

Hibernate & Design PatternsA Practical Overview

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

Page 2: Hibernate & Design Patterns A Practical Overview

Powered by 2

Agenda

•Design Patterns

–Transfer Objects

–Data Transfer Objects

–Factories

•Hibernate Framework

•Connecting the pieces

Page 3: Hibernate & Design Patterns A Practical Overview

PART IDesign Patterns & Hibernate

Page 4: Hibernate & Design Patterns A Practical Overview

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

Page 5: Hibernate & Design Patterns A Practical Overview

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

Page 6: Hibernate & Design Patterns A Practical Overview

Powered by 6

Transfer Objects

• A Transfer Object is just

a Serializable POJO

(Plain Old Java Object)

• Also called Persistent

POJO or Valuable Object

Page 7: Hibernate & Design Patterns A Practical Overview

Powered by 7

Indentify Class “Version”.

Calculated Hash using the attributes

names and methods

signatures. It’s not a random number!

Page 8: Hibernate & Design Patterns A Practical Overview

Powered by 8

Updatable Transfer Object Strategy

Page 9: Hibernate & Design Patterns A Practical Overview

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

Page 10: Hibernate & Design Patterns A Practical Overview

Powered by 10

Data Access Objects

Page 11: Hibernate & Design Patterns A Practical Overview

Powered by 11

Data Access Objects

•Implement the CRUD operations:

–public int insertResource(…)

–public boolean deleteResource(…)

–public ResourceTO findResource(…)

–public boolean updateResource(…)

Page 12: Hibernate & Design Patterns A Practical Overview

Powered by 12

DAO – We want more flexibility

Concrete Factory

DAO – Implements the operation for a specific

Entity

Abstract Factory

Page 13: Hibernate & Design Patterns A Practical Overview

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

Page 14: Hibernate & Design Patterns A Practical Overview

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 )

Page 15: Hibernate & Design Patterns A Practical Overview

Powered by 15

Hibernate Framework

• It provides:

– Transparent Persistence

– SQL generation

– Support for Transaction

Page 16: Hibernate & Design Patterns A Practical Overview

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)

Page 17: Hibernate & Design Patterns A Practical Overview

Powered by 17

A connection configuration filehibernate.cfg.xml

Page 18: Hibernate & Design Patterns A Practical Overview

Powered by 18

Page 19: Hibernate & Design Patterns A Practical Overview

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

Page 20: Hibernate & Design Patterns A Practical Overview

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

Page 21: Hibernate & Design Patterns A Practical Overview

Powered by 21

Page 22: Hibernate & Design Patterns A Practical Overview

PART IIConnecting the Pieces

Page 23: Hibernate & Design Patterns A Practical Overview

Powered by 23

Just Remembering

Page 24: Hibernate & Design Patterns A Practical Overview

Powered by 24

A Simple ER

Page 25: Hibernate & Design Patterns A Practical Overview

Powered by 25

A Employee Hibernate Mapping FileEmployeeTO.hbm.xml

Page 26: Hibernate & Design Patterns A Practical Overview

Powered by 26

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

Page 27: Hibernate & Design Patterns A Practical Overview

Powered by 27

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

Page 28: Hibernate & Design Patterns A Practical Overview

Powered by 28

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

Page 29: Hibernate & Design Patterns A Practical Overview

Powered by 29

Page 30: Hibernate & Design Patterns A Practical Overview

Powered by 30

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

Page 31: Hibernate & Design Patterns A Practical Overview

Powered by 31

Page 32: Hibernate & Design Patterns A Practical Overview

Powered by 32

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

Page 33: Hibernate & Design Patterns A Practical Overview

Powered by 33

So we got a workspace structure like this…

Page 34: Hibernate & Design Patterns A Practical Overview

Powered by 34

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

Page 35: Hibernate & Design Patterns A Practical Overview

Powered by 35

A deeper look attestInsertDepartment()

Page 36: Hibernate & Design Patterns A Practical Overview

Powered by 36

A deeper look attestFindDepartmentById()

Page 37: Hibernate & Design Patterns A Practical Overview

Powered by 37

A deeper look attestListDepartments()

Page 38: Hibernate & Design Patterns A Practical Overview

Powered by 38

A deeper look attestFindDepartmentByName()

Page 39: Hibernate & Design Patterns A Practical Overview

Powered by 39

A deeper look attestUpdateDepartment()

Page 40: Hibernate & Design Patterns A Practical Overview

Powered by 40

A deeper look attestDeleteDepartment()

Page 41: Hibernate & Design Patterns A Practical Overview

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 )

Page 42: Hibernate & Design Patterns A Practical Overview

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

)

Page 43: Hibernate & Design Patterns A Practical Overview

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

BSc. Thiago OliveiraTechnical Manager

[email protected]

43