Ch01 Introduction to Persistence in Java

download Ch01 Introduction to Persistence in Java

of 19

Transcript of Ch01 Introduction to Persistence in Java

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    1/19

    Akin [email protected]

    http://www.javaturk.orgDecember 2010

    mailto:[email protected]://www.javaturk.org/http://www.javaturk.org/mailto:[email protected]
  • 8/8/2019 Ch01 Introduction to Persistence in Java

    2/19

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    3/19

    Objects, Always

    With the advent of the concept of object in 60s and 70s in firstSimula and then Smalltalk we started thinking in object-oriented terms

    Then came Java in the mid of 90s and object-oriented thinkinginvaded our mind (even though we have a long way to make it

    a new silver bullet in Software Engineering!)When we start talking about a new project we can not help

    thinking in terms of objects

    We can think of objects at different phases of the projects withdifferent concerns

    We model requirements in terms of objects We design our solution in terms of objects

    We implement the software in an object-oriented language such as Java

    etc.

    Well, at least we hope for this

    3

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    4/19

    Storing Objects

    But you know all good things eventually come to an end

    How do you store your objects, make them persistent?

    What are our options?Serializing objects to files or marshalling them to XML files

    Persisting the objects to object-oriented databases

    I wish we could

    Persisting the objects to relational databases Most of the time this is the only option

    4

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    5/19

    Why RDMSs?

    It seems that storing our objects to a relational databasemanagement system (RDMS) will be our only choice forlonger times because

    RDMS are ubiquitous

    RDMS are very strong, skillful and mature in terms of ourenterprise needs

    RDMS producers such as Oracle, IBM are very big, strongand well-known companies

    Playing with objects stops when you stand in front of thehigh and fortified walls of relational databases and wemust obey their rules

    5

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    6/19

    Object-Relational Mismatch

    When you decide to store your objects into a RDMS youhave to answer many questions such as

    How to represent each object in the database knowing thefact that your objects have complex compositional and

    hierarchical relations among themselvesHow do you handle CRUD (Create-Read-Update-Delete)

    operations for the objects?

    How do you handle types differences between your codeand the database?

    How do you minimize the database access for performanceand scalability purposes?

    How about application-side caching?

    Transactions, etc.

    6

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    7/19

    Points of Mismatch

    Aims and thus structures of object-oriented languages and

    databases are totally different

    So they are not congruent mainly on following very basic

    issues:

    Encapsulation

    Types

    Structure

    IntegrityManipulation

    Transaction

    This is called object-relational mismatch

    7

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    8/19

    Object-Relational Mapping ORM

    Percentage of persistence effort for the objects in the projectsthat develop highly-transactional business software is high

    Thus from the earlier times people started writing their own

    framework or tool to automate object-to-table translation orbought one of a few, highly-expensive commercial tools

    They aimed to make their persistence framework part of theirenterprise architecture so that in the long run the persistenceeffort in the projects can be smaller and more predictable

    Process that manage object-relational mismatch and automatedesign-time mapping along with run-time CRUD operations arecalled Object-Relational Mapping or ORM

    8

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    9/19

    ORM Tools

    In 90s people tried to write its own ORM tools because

    commercial ORM products were not many in number and

    highly expensive.

    We can divide history of commercial ORM products into

    two different era:

    Pre-Java ORM tools

    Java ORM Tools

    9

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    10/19

    Pre-Java ORM Tools

    Some of the pre-Java ORM tools

    Delphi

    Rogue Wave DBTools.h++ (SourcePro DB now)

    NeXT DBKit

    Toplink for Smalltalk

    IBM ObjectExtender

    10

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    11/19

    Java ORM Tools: First Generation

    JDBC 1.0 and 2.0

    JavaBlend

    Toplink for Java

    EJB 1.0

    VisualAge Persistence Builder

    EJB 2.0

    11

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    12/19

    Java ORM Tools: Second Generation

    iBatis in 2001

    Castor in 2002

    Hibernate in 2002

    JDO in 2002 through JSR-12

    Java Persistence API (JPA) in 2006, through JSR-220

    12

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    13/19

    Java Persistence API

    Java Persistence API (JPA) 1.0 was released as part of EJB

    3.0 in Java EE 5 in May, 2006

    JSR-220: http://jcp.org/en/jsr/detail?id=220

    Java Persistence API (JPA) 2.0 was released in December

    2009 as part of Java EE 6

    JSR-317: http://www.jcp.org/en/jsr/detail?id=317

    13

    http://jcp.org/en/jsr/detail?id=220http://www.jcp.org/en/jsr/detail?id=317http://www.jcp.org/en/jsr/detail?id=317http://www.jcp.org/en/jsr/detail?id=317http://jcp.org/en/jsr/detail?id=220http://jcp.org/en/jsr/detail?id=220
  • 8/8/2019 Ch01 Introduction to Persistence in Java

    14/19

    After JPA

    With the advent of JPA, existing ORM products have

    relased their JPA versions:

    For example Hibernate and Toplink have two ORM products,

    one with native API and the other one is the implementation

    of JPA

    Some of the JPA products in the market are

    Hibernate Entity Manager

    Toplink

    EclipseLink (part of Toplink and donated by Oracle)

    OpenJPA

    14

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    15/19

    Popular JPA Pages

    JSR-220: http://jcp.org/en/jsr/detail?id=220

    JSR-317: http://www.jcp.org/en/jsr/detail?id=317

    Main page at Oracle

    http://www.oracle.com/technetwork/java/javaee/tech/persiste

    nce-jsp-140049.html

    15

    http://jcp.org/en/jsr/detail?id=220http://www.jcp.org/en/jsr/detail?id=317http://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.htmlhttp://www.jcp.org/en/jsr/detail?id=317http://www.jcp.org/en/jsr/detail?id=317http://jcp.org/en/jsr/detail?id=220
  • 8/8/2019 Ch01 Introduction to Persistence in Java

    16/19

    Why ORM?

    Persistence efforts eat significant amount of resource in

    software projects

    ORM provides

    Totally object-oriented approach

    Concentration on business not plumbing code

    Comprehensive solution to persistence layer

    Higher productivity

    Less code writtenPersistence made easy

    Almost no coupling with database

    High performance through object-caching

    16

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    17/19

    Why Not ORM?

    There are some down sides of ORMs:

    Learning curve

    Choosing the best implementation

    Think about differences among the implementations

    Poor performance possibility due to implementation choices

    Possible cost of extra CPU, memory, database usage

    May not suitable for everything:

    Reporting, data migration, etc.Working with data that serves to legacy applications

    17

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    18/19

    Approaches to ORM

    How should one start ORM in a project?

    Bottom-up

    Top-down

    Meet-in-the-middle

    18

  • 8/8/2019 Ch01 Introduction to Persistence in Java

    19/19

    Dont Forget!

    And when using ORM tools dont forget about

    Patterns and idioms

    Best practices

    19