Title: ObjectOriented Persistence Techniques Using OSS
1Object-Oriented Persistence Techniques Using
OSS
2Overview
- Understanding the Problem
- Database Refresher
- Object-Oriented Persistence
- In .NET
- In Java (Hibernate)
3Understanding the Problem
- Software exists to solve a problem!
- Every SE professor at this school
Image from plg.uwaterloo.ca/ migod/fun/
4The Good Old Days
- In the early days of programming
- Hardware presented limitations
- Fewer clock cycles to solve the problem
- Make best use of RAM and other resources
- Efficiency was critical!
- Programming teams were smaller
- Many programs written by a single person
- Maintenance generally consisted of bug-fixes
Image from www.upss.com/menus.htm
5Software Evolved
- All these GOTOs are hard to follow!
- Structural programming presents a more
understandable flow of logic - This main() is too long!
- Functional programming allows code to be split up
into smaller parts - I want to reuse parts of this program!
- Object-oriented programming abstracts data and
methods into highly reusable data structures
6Transitioning to OOWhy did it happen?
- Maintenance is a growing concern
- Project groups are getting larger
- Code not always maintained by original author
- Integrated, robust software in demand
- Efficiency is good, but
- Current hardware alleviates many efficiency
concerns - Tricks made in the interest of increased
efficiency are often unintelligible and hinder
maintainability
7The Point
- The evolution of software has stressed
maintainability over efficiency by employing
objects - Not all attributes simple, can contain other
objects - Polymorphism
- The fastest programs are NOT object-oriented!
8The Point
- The evolution of relational database software has
stressed parallelization and scalability over
object-oriented support - Transactional-based processing to allow
multiprocessing without corruption of data - Many database packages charge based on
multiprocessor utilizations - Can only store SIMPLE data types!!
- Strings
- Dates
- Integers
9The Problem
- Object-oriented programming solutions
- Efficiency-based database solutions
- How can we persist our complex, polymorphic
objects in these relational database systems in a
maintainable, object-oriented fashion?
10Overview
- Understanding the Problem
- Database Refresher
- Object-Oriented Persistence
- In .NET
- In Java (Hibernate)
11Umm Whats Persistence?
- 3 types of data in an application
- Transient
- Persistent
- Detached
Information from http//www.hibernate.org/hib_docs
/v3/reference/en/html/objectstate.html
12Transient Data
- Does not need to be saved or retained after
program exits - Does not contribute significant amount of state
to the object - Often temporary or derived information, and often
local to a function
13Persistent Data
- Needs to be saved or retained after program
exits - Contributes a significant amount of state to the
object - Often all private class variable for a data
object contain persistent data - Persistent data used to reconstruct the saved
object after it has been saved
14Detached Data
- Data that was loaded from a database but has
since been changed or copied - Out of synch with the database
- Can create undesirable states
15K, Whats a Relational Database?
- A relational database is a collection of data
items organized as a set of formally-described
tables from which data can be accessed or
reassembled in many different ways without having
to reorganize the database tables.http//searc
horacle.techtarget.com/sDefinition/0,,sid41_gci212
885,00.html
16A Database Looks Like
17Relational Database Structure
- Server A running instance of the database
software. Each server has multiple - Databases Contain a set of isolated stored
procedures, functions, and multiple - Tables (next page)
18Relational Database Structure
- Table An entity that stores a set of data.
- Columns Each stores a single attribute, or
field - Rows Each row is a record, or an instance of
data in its corresponding table
19Tables
20Normalization
- Not all information about a system can be
persisted in a single table - Not all information about a system is unrelated
- Information is normalized across several tables
- Related information is linked via keys
21Relationships
Image from www.koffice.org/kexi/
22Database Terms
- Connection String A string of characters that is
used in establishing a connection to a database - SQL Structured Query Language, a protocol used
to specify information desired from a database - Join A means of combining the contents of
records from different tables based on certain
criteria - Key A field or set of fields on a table for
which the table is constrained in such a way that
no two records can coexist in the table with the
same values
Terms from Hibernate in Action
23Database Terms
- Record One row of a table or resultset.
- Query An instance of a SQL statement that has
been sent to a database to be performed - Resultset The results of a query. All desired
fields of all records that fit the criteria in
the query are returned - Serialization The conversion of an object into a
meaningfully formatted string for which the
object could use to reload its state at the time
of conversion
Terms from Hibernate in Action
24Overview
- Understanding the Problem
- Database Refresher
- Object-Oriented Persistence
- In .NET
- In Java (Hibernate)
253-Tiered Development
- Presentation Layer
- GUI
- Business Logic Layer
- Policy layer
- Monkeys!
- Data/Services Layer
- Database interactions
- Focus is on making Data Layer as
- object-oriented as the rest of the system
Picture and terms from http//www.officewizard.com
/books/clientserver/ClientServerComputing.htm
26Challenges of an OO Data Layer
- Normalization
- Tables are split up to reduce redundant data
- Challenges developers capacity to treat each
table as an object - Composition
- Objects contain other objects
- Contained objects must be stored in separate
table - Inheritance
- Store inherited objects in
- Separate table?
- Same table, separate table for additional
attributes? - Same table, make table include attributes for all
descendents?
27Pre-.NET Database Interfacing
- ADO
- ActiveX Data Objects
- Supplied with VB6 for classic ASP applications
- Set of objects used in interfacing with a
database - Link to database remained open while iterating
through the returned data - Implemented using DOM
Terms from http//www.vbwm.com/articles/2002/tcole
/adonet1/
28.NET Database Interfacing
- ADO.NET heavily revamps traditional ADO
- Disconnected
- On a transaction a connection is opened, data is
retrieved, and connection is automatically closed - Saves server and database resources!
- Implemented using XML
- VERY easy for ADO.NET objects to read XML or to
serialize contents into XML - Doesnt assume one size fits all
- DataReader
- DataSet
Terms from http//www.vbwm.com/articles/2002/tcole
/adonet1/
29.NET Database Interfacing
- DataReader
- Lightweight and quick
- Should be used for quick and easy database
queries - Able to quickly and efficiently iterate over a
resultset - Read-only
- Often used to bind datagrids
Terms from http//www.vbwm.com/articles/2002/tcole
/adonet1/
30.NET Database Interfacing
- DataSet
- Heavyweight but powerful
- Each query places the resultset in a table in
the DataSet - Can join tables within the DataSet
- Can query the DataSet directly to filter
records - Quick and easy conversion to and from XML
- Great for XML parsing and persistence!
Terms from http//www.vbwm.com/articles/2002/tcole
/adonet1/
31.NET Database Interfacing
- DataAdapter
- Mediates connection between DataReader/DataSet
and the database - Stores important database information
- Connection string
- SQL query to be sent
- Name of stored procedure (to be discussed)
- Parameters
Terms from http//www.vbwm.com/articles/2002/tcole
/adonet1/
32Code!
Sub LoadCustomers() Dim myConnection As New
OleDb.OleDbConnection( _
"ProviderMicrosoft.Jet.OLEDB.4.0 Data
SourceC\Program _ Files\Microsoft
Visual) Dim myDataAdapter As New
OleDb.OleDbDataAdapter( _ "Select from
Customers", myConnection) Dim myDataSet As New
DataSet() ' Fill DataSet with table, call it
'Customers' myDataAdapter.Fill(myDataSet,
"Customers") ' Display first CompanyName
field of first row in Customers table
MessageBox.Show(myDataSet.Tables("Customers").Rows
(0)("CompanyName")) End Sub
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
33But Josh Doesnt Like It
Sub LoadCustomers() Dim myConnection As New
OleDb.OleDbConnection( _
"ProviderMicrosoft.Jet.OLEDB.4.0 Data
SourceC\Program _ Files\Microsoft
Visual)
- Connection String
- Hard coded
- Connection Strings are volatile
- Change when switch from development to production
databases - Make private constant for the class?
- Not good enough Connection strings are
generally constant throughout a project - Will end up redefining the same constant in every
class - Solution Project-wide constant
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
34But Josh Doesnt Like It
Sub LoadCustomers() Dim myConnection As New
OleDb.OleDbConnection(CONSTANTS.CONN_STRING)
Dim myDataAdapter As New OleDb.OleDbDataAdapter(
_ "Select from Customers",
myConnection) Dim myDataSet As New DataSet()
' Fill DataSet with table, call it
'Customers' myDataAdapter.Fill(myDataSet,
"Customers") ' Display first CompanyName
field of first row in Customers table
MessageBox.Show(myDataSet.Tables("Customers").Rows
(0)("CompanyName")) End Sub
- Replaced hard-coded connection string
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
35But Josh Doesnt Like It
Sub LoadCustomers() Dim myConnection As New
OleDb.OleDbConnection(CONSTANTS.CONN_STRING)
Dim myDataAdapter As New OleDb.OleDbDataAdapter(
_ "Select from Customers", myConnection)
- Query mixed in with the code!
- Modern databases offer stored procedures to aid
in partitioning the Data Layer - Stored procedures offer many advantages over
formulating a query in-code
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
36Stored Procedures
- Set of pre-written SQL statements that reside in
the database - Code references the stored procedure instead of
building the SQL statement - Advantages
- Compilation
- Maintainability
Terms from http//www.vbwm.com/articles/2002/tcole
/adonet1/
37Stored Procedures
- Compilation
- Compilation of stored procedures does NOT
produce byte code! - Syntax checking done when saving the stored
procedure - You will not be allowed to save a stored
procedure that does not compile - Catch query errors at compile time rather than
run time! - Bonus! Compilation allows speeds up queries
38Stored Procedures
- Maintainability
- Stored procedures can be reused throughout an
application - If the database changes, only need to update each
affected query once in the stored procedure - Readability enhanced
- Syntax highlighting
- Dont need to escape as many characters since the
procedure does not need to be encased in a string
39Stored Procedures
40But Josh Doesnt Like It
Sub LoadCustomers() Dim myConnection As New
OleDb.OleDbConnection(CONSTANTS.CONN_STRING)
Dim myDataAdapter As New OleDb.OleDbDataAdapter(
_ GET_CUSTOMERS myConnection)
myDataAdapter.SelectCommand.CommandType
CommandType.StoredProcedure Dim myDataSet As
New DataSet() ' Fill DataSet with table,
call it 'Customers' myDataAdapter.Fill(myDataS
et, "Customers") ' Display first
CompanyName field of first row in Customers table
MessageBox.Show(myDataSet.Tables("Customers").
Rows(0)("CompanyName")) End Sub
- Replaced SQL with stored procedure
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
41But Josh Doesnt Like It
Dim myConnection As New OleDb.OleDbConnection(C
ONSTANTS.CONN_STRING) Dim myDataAdapter As New
OleDb.OleDbDataAdapter( _ GET_CUSTOMERS
myConnection) myDataAdapter.SelectCommand.Comma
ndType CommandType.StoredProcedure Dim
myDataSet As New DataSet() ' Fill DataSet
with table, call it 'Customers'
myDataAdapter.Fill(myDataSet, "Customers")
- This code is reusable and non-OO
- Developers care about objects, not records in a
DataSet! - Instead of repeating the code every time we want
the customers in the database, lets make a
function that returns the resultset as objects of
the expected type
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
42But Josh Doesnt Like It
Function getCustomer(ByVal id As Integer) As
Customer Dim myConnection As New
OleDb.OleDbConnection(CONSTANTS.CONN_STRING)
Dim myDataAdapter As New OleDb.OleDbDataAdapter(
_ GET_CUSTOMER myConnection)
myDataAdapter.SelectCommand.CommandType
CommandType.StoredProcedure
myDataAdapter.Parameters.Add(_at_id, id) Dim
myDataSet As New DataSet() ' Fill DataSet
with table, call it 'Customers'
myDataAdapter.Fill(myDataSet, "Customers")
String CompanyName myDataSet.Tables(Customers
) _
.Rows(0).Item(CompanyName) Customer
customer new Customer (CompanyName, )
Return customer End Function
- This code is reusable and non-OO
- Developers care about objects, not records in a
DataSet! - Instead of repeating the code every time we want
the customers in the database, lets make a
function that returns the resultset as objects of
the expected type
Code from from http//www.developer.com/net/vb/art
icle.php/10926_1540311_7
43Object-Oriented Persistence in .NET
- .NET offers powerful ADO.NET objects
- By defining constants, employing stored
procedures, and isolating Data Layer operations
into object-oriented functions, we can emulate an
object-oriented database from a relational one
44Overview
- Understanding the Problem
- Database Refresher
- Object-Oriented Persistence
- In .NET
- In Java (Hibernate)
45Java Persistence
- Wouldnt it be great if all you had to do to save
an object to the database was say
session.save(customer)
46Introducing Hibernate!
- Hibernate
- Open-source framework
- Sits between Policy and Data layer
- Transforms the frontend of a relational database
into an object-oriented database
Information from Professional Hibernate
47How Does it Work?
- Installation
- JAR file with classpath
- Supporting files
- hibernate.properties
- hibernate.cfg.xml
Information from Professional Hibernate
48How Does it Work?
- hibernate.properties
- Non-XML format
- Discrepancies between this and hibernate.cfg.xml
lose out in favor of hibernate.cfg.xml - Will likely be depracated in the near future
Information from Professional Hibernate
49How Does it Work?
- hibernate.cfg.xml
- XML format
- Contains all the information needed to run
Hibernate - This includes
- Connection String / JDBC driver information
- Need only be in ONE place in the project!
- Database dialect and other settings
- Class-to-table mappings or locations to
class-to-table mapping files
Information from Professional Hibernate
50How Does it Work?
- Database dialect
- Dialect A form of SQL that is constrained or
contains proprietary keywords or formats that are
specific to certain databases - Since Hibernate takes care of much of the SQL, it
must know correct dialect for the database
Information from Professional Hibernate
51How Does it Work?
- Class-to-table mappings
- Tells Hibernate what classes to persist, what
types each field is, and where all the
information should be persisted. - Tells Hibernate how to generate IDs
- Provide FREE DOCUMENTATION on how the
relationship between object-oriented data in the
application and the corresponding data in the
database!
Information from Professional Hibernate
52Example
lthibernate-configurationgt lt!-- a
SessionFactory instance listed as /jndi/name --gt
ltsession-factory name"javahibernate/S
essionFactory"gt lt!-- properties --gt
ltproperty name"connection.datasource"gtjava/co
mp/env/jdbc/MyDBlt/propertygt ltproperty
name"dialect"gtorg.hibernate.dialect.MySQLDialectlt
/propertygt ltproperty name"show_sql"gtfalse
lt/propertygt ltproperty name"transaction.fa
ctory_class"gt org.hibernate.transactio
n.JTATransactionFactory lt/propertygt
ltproperty name"jta.UserTransaction"gtjavacomp/U
serTransactionlt/propertygt lt!-- mapping
files --gt ltmapping resource"org/hibernate
/auction/Item.hbm.xml"/gt ltmapping
resource"org/hibernate/auction/Bid.hbm.xml"/gt
lt!-- cache settings --gt
ltclass-cache class"org.hibernate.auction.Item"
usage"read-write"/gt ltclass-cache
class"org.hibernate.auction.Bid"
usage"read-only"/gt ltcollection-cache
collection"org.hibernate.auction.Item.bids"
usage"read-write"/gt lt/session-factorygt lt/hi
bernate-configurationgt
53Hibernate and SQL
- HQL (Hybernate Query Language)
- Hibernate offers a simplified query language
- Very similar to traditional SQL
- Uses dialects to convert the HQL into appropriate
SQL - SQL
- Hibernate also allows straight SQL statements to
be executed - Good for optimizing queries that Hibernate
doesnt compile efficiently
Information from Professional Hibernate
54Sessions
- Each time a database interaction is required, a
session must be used. - Sessions created from the Singleton
SessionFactory class - Types of Database Interactions
- Select
- Load
- Insert
- Update
- Delete
Information from Professional Hibernate
55Select
- What is it?
- Query that creates a resultset that contains the
specified columns for all records that meet a
criteria - How does Hibernate make it easy?
try Session session
sessionFactory.openSession() List users
session.find(from Users) // HQL
session.close() catch (Exception e)
Information from Professional Hibernate
56Load
- What is it?
- Query that retrieves a unique record from a table
that has the same ID - How does Hibernate make it easy?
// ID initialized elsewhere try
User myUser new User() // blank Session
session sessionFactory.openSession()
List users session.load(myUser, ID) // HQL
session.close() catch (Exception e)
Information from Professional Hibernate
57Insert
- What is it?
- Query that adds a new record to a table with
specified properties - How does Hibernate make it easy?
User myUser new User(Josh, Schendel,
schendej) try Session session
sessionFactory.openSession()
session.save(myUser) // HQL
session.close() catch (Exception e)
Information from Professional Hibernate
58Update
- What is it?
- Query that changes the record in the database
that has same ID as the object - How does Hibernate make it easy?
//User loaded from database
myUser.setName(Mark) try Session
session sessionFactory.openSession()
session.update(myUser) // HQL
session.close() catch (Exception e)
Information from Professional Hibernate
59Delete
- What is it?
- Query that removes a record that has the same ID
from the database - How does Hibernate make it easy?
//User loaded from database try
Session session sessionFactory.openSession()
session.delete(myUser) // HQL
session.close() catch (Exception e)
Information from Professional Hibernate
60Final Notes About Hibernate
- Simplifies object persistence dramatically
- Supports more complicated queries
- Mapping documents
- Written once
- Used everywhere to simplify persistence
- Fully support all joins and most SQL transactions
via dialects - Object-oriented interface to the database without
the cost of an object-oriented database
Information from Professional Hibernate
61Bibliography
Bauer, Christian, and King, Gavin. Hibernate in
Action. Manning Publishing. 2005
Iverson, Will. Hibernate, a J2EE Developers
Guide. Pearson. 2005
Minter, Dave, and Linwood, Jeff. Pro Hibernate
3. Wiley Publishing. 2005
Pugh, Eric, and Gradecki, Joseph. Professional
Hibernate. Wiley Publishing. 2004
HIBERNATE - Relational Persistence for Idiomatic
Java. 3.11. 2/23/06. lthttp//www.hibernate.org/
hib_docs/v3/reference/en/html/objectstate.htmlgt
Visual Basic Web Magazine. Cole, Tom. 2/23/06.
lthttp//www.vbwm.com/articles/2002/tcole/adonet1/gt