Title: CISH6960: Developing Enterprise Applications
1CISH-6960 Developing Enterprise Applications
2Persistence
- Object / Relational Mapping
3Outline
- Roll your own persistence layer
- Design patterns
- Abstract Factory
- Builder
- Strategy
- Data Access Object
- Java Data Objects
- Hibernate
4Persistence Layer
Persistence Layer
Relational Data
Object Model
5Persistence Layer
- Object / Relational mismatch
- Object model based on maintenance
- Relational model based on data integrity
- Impedance mismatch
- Translation issues at the boundary
- Confine changes to a layer
- Ease of maintenance
- Can switch persistence mechanisms
6Simple Mapping
- Map classes to database tables
- One to one mapping
- Each class becomes a table
- Each attribute becomes a column
- Individual rows are objects
7Simple Mapping
public class Employee private int id private
String firstName private String
lastName private String email public
Employee() // ... // Gets and sets //
Other methods public void work() // ...
8Simple Mapping
- Multiple classes
- Employee to Address
- One-to-one
- Employee to Department
- Many-to-one
- Employee to Project
- Many-to-many
- Object model uses reference attributes
- Databases use tables
- Foreign keys for relationships
- Cross-reference tables for normalization
9Simple Mapping
- Inheritance
- Does not map well to relational structures
- Can add type field to table
- 1 Salaried, 2 Hourly, etc
- Can create separate tables for each subclass,
with foreign keys to superclass - Requires joins, which take time
- Arent really any great solutions
10Requirements
- Ease of transformation
- Query mechanism to return objects from data
- Transaction management
- Scalability and performance
- Portability across changes in schema, vendors,
storage technology - Ease of development, configuration, testing
11Object Serialization
- Flatten object graph into bytes
- Serialization follows reference attributes
- Simple library classes available
- Business classes must implement Serializable
- Wrap sources inside ObjectInputStream and
ObjectOutputStream - Simple writeObject() method
- Simple readObject() method
- Keyword transient
- Limited to Java
- Lack of querying capabilities
12Design Patterns
- Factory Method
- Objects instantiated through a method
- Return type is interface or abstract class
- Client is shielded from object type
- Abstract Factory
- Create families of related objects without
specifying the actual classes
13Abstract Factory
14Data Access Object
- From Core J2EE Patterns
- Abstract and encapsulate all access to the
persistent store - DAO manages connection with the data store to
obtain and store data
15Data Access Object
16Data Access Object
17DAO Factory
18Data Access Object
public class Client public static void
main(String args) DAOFactory fact
DAOFactory.getDAOFactory(DAOFactory.FI
LE) CustomerDAO custDAO fact.getCustomerDAO()
Customer c1 new Customer("Fred","Flintst
one","fflint_at_bedrock.org") custDAO.insertCustom
er(c1) Customer c2 new
Customer("Barney","Rubble","brubble_at_bedrock.org")
custDAO.insertCustomer(c2) Customer c3
custDAO.findCustomer(c2.getName()) custDAO.dele
teCustomer(c3) Customer c4 new
Customer("Mr.","Slate","boss_at_slate.com") custDA
O.insertCustomer(c4) c1.setEmail("fred.flintsto
ne_at_bedrock.org") custDAO.updateCustomer(c1)
19Design Patterns
- Builder
- Define a class whose purpose is to assemble
instances of another class - Can enforce constraints
- Builds in complex construction rules
- Can be used to make SQL more object oriented
20Builder Pattern
21Builder Pattern for SQL
22Design Patterns
- Strategy
- Delegate responsibility to one of several
possible implementations - Each strategy represents a set of behaviors
- Strategy can be set at run time
- Flexible
23Strategy Pattern
24Strategy Pattern
- Handle different databases
- Each DB has its own implementation
- Methods all the same
25Strategy for SQL
26Design Patterns
- Façade
- Provide a simplified interface to a complex set
of classes - For SQL, use a single class to wrap queries
- Façade hides Connection, Statement, ResultSet
- Clients interact with the Façade
27Façade Pattern for Queries
28Java Data Objects
- Specification working through the community
process (JSR 12) - Store POJOs in any kind of storage, transparent
to the user - Specify implementation details in a properties
file
29Java Data Objects
JDOHelper
PersistenceManagerFactory
PersistenceManager
Query
Transaction
30JDO
- PersistenceManagerFactory used to acquire an
instance of a PersistenceManager - PersistenceManager used to get a reference to a
Transaction or Query - Transactions have standard methods
- begin()
- commit()
- rollback()
31JDO
- Source code enhancement
- Java classs source code is modified before it is
compiled - Byte code enhancement
- Post compiled class file is modified
- Binary compatibility required across JDO
implementations
32JDO Query Language
- JDO defines JDOQL
- Simple, object based SQL
- Filtering based on booleans and objects
- Query q persistenceMgr.newQuery( Employee.class
, firstName\John\) - Call execute() method
- Returns all employees whose first name is John
33JDO
- Also defines extents to refer to all instances
of a class in the data store - Returns a java.util.Iterator
- Can even return instances of subclasses as well
- Hollow objects return references without
loading data
34Hibernate
- Open source framework
- No byte or source code enhancing
- Set up configuration files
- One file to define data store info
- One file for each class
- Can be combined into a single mapping file
- Again defines its own query language
- HQL, Hibernate Query Language
35Persisting Components
- Enterprise JavaBeans
- Entity beans are an object model on the data
store - Secure, transactional, distributed,
- More next time