JDO PRESENTASI

Embed Size (px)

Citation preview

  • 8/6/2019 JDO PRESENTASI

    1/26

    Java Data Objects (JDO)

    Overview and Future

    Michael Vorburger, Vertical*i

    Java User Group SwitzerlandZurich, 31.03.05

  • 8/6/2019 JDO PRESENTASI

    2/26

    2005-03-31 2

    Agenda

    Object Persistence

    O/R Mapping (ORM)

    History

    JDO Introduction

    JDO API

    JDOQL

    JDO 2.0 (JSR 243)

    JDO Implementations(Vendors & OSS)

    Using JDO in EJB 2.1

    Using JDO in Web Apps

    Using JDO and/or JDBC

    EJB 3.0 (JSR 220)

    Future

  • 8/6/2019 JDO PRESENTASI

    3/26

    2005-03-31 3

    About Speaker

    Personal interest in persistence space

    Culprit for an in-house ORM layer

    Somehow picked up JDO in 2001

    Core Java Data Objects co-author

    For past ca. 3 years user of JDO

  • 8/6/2019 JDO PRESENTASI

    4/26

    2005-03-31 4

    About

    Audience

    How many of you would consider

    themselves moderately familiar with

    JDO, or another similar persistence

    framework, anyway?

    How many of you have read the JDO2.0 and EJB 3.0 Persistence Specs?

  • 8/6/2019 JDO PRESENTASI

    5/26

    2005-03-31 5

    Object Persistence Concepts Graph of data objects (Domain Model)

    API: persist(), Persistence by Reachability

    Transactions

    Query!

    Internal: Memory Management, Caching, etc.

  • 8/6/2019 JDO PRESENTASI

    6/26

    2005-03-31 6

    O/R Mapping (ORM)Impedance Mismatch of OOP & RBDMS:

    Classes vs. Tables, Inheritance mapping strategies

    Relationships: Single 1-1 or 1-M, Multiple M-N, Linkwith attributes DBA mixed data and relationship,Inverses/Bi-directionality, Lists, Maps

    Identity Management, Uniqueness

    Class Attribute vs. Column Type Mappings

    More in Core JDO Book Chapter2, but: Solutions for

    mismatches exist; see also your ORM Vendor Doc!

  • 8/6/2019 JDO PRESENTASI

    7/26

    2005-03-31 7

    History Few things fall from the sky

    object persistence in general

    and JDO in particular buildupon a long history

    Have you heard of any of

    these names before?

    CORBA POS

    Persistent Object

    Service Project Forest of Sun

    Labs (OPJ, PJama)

    ODMG API

    Gemstone, O2,

    TopLink (15y+)

    (EJB 2.1 Entity Beans)

  • 8/6/2019 JDO PRESENTASI

    8/26

    2005-03-31 8

    JDO Introduction JDO is an interface-based API for selection and

    transformation of transactional persistent storagedata into native Java programming language

    objects Transparent Object Persistence!

    Various implements for this API exist: Most as ORMfor your favourite RDB; some for standalone or

    built-in OODBMS.

    JDO 1.0 (JSR 12) in 2003

    JDO 2.0 (JSR 243) Extension, accepted 1m ago

  • 8/6/2019 JDO PRESENTASI

    9/26

    2005-03-31 9

    JDOA

    PI: PMF & PM PersistenceManagerFactory pmf =

    JDOHelper.getPersistenceManagerFactory(Properties p);

    javax.jdo.PersistenceManager pm =pmf.getPersistenceManager();

    pm.makePersistent(customer);

    customer = pm.getObjectById(oid,true);

  • 8/6/2019 JDO PRESENTASI

    10/26

    2005-03-31 10

    JDO Metadata & Enhancement JDO XML metadata describes which classes can be

    persisted (), with relevant options(, )

    JDO implementation run-time needs to interact withpersistence instances for lifecycle notification, load,store/flush. Generally, this is achieved by running abuild-time tool, a JDO [byte code] enhancer. (It addsthe PersistenceCapable interface from SPI API topersistent classes. This is no longer strictly for JDOcompliance,see BinaryCompatibility.)

    Subject of hot debates strange, think AOP etc.

  • 8/6/2019 JDO PRESENTASI

    11/26

    2005-03-31 11

    JDOA

    PI: Transactions Non-Managed

    javax.jdo.Transaction jtx =pm.currentTransaction();

    jtx.begin(); /* Do Stuff */ jtx.commit();

    Managed (usually in J2EE container) JTAAPI

    javax.transaction.UserTransaction ut;

    ut = {lookup somewhere, e.g. JNDI};ut = ejbContext.getUserTransaction()

    ut.begin(); /* Do Stuff */ ut.commit();

    Declarative EJB container managed transactions

    Core JDO Book Chapter 11

  • 8/6/2019 JDO PRESENTASI

    12/26

    2005-03-31 12

    JDOA

    PI: Query and JDOQL Query q = pm.newQuery(Customer.class,

    "customerName.startsWith('Tintin') &&lastOrder.price > 100");

    Collection results = (Collection)q.execute();Iterator it = results.iterator();while (it.hasNext()) {

    Customer cust = (Customer)it.next());

    Parameters: q.declareParameters(String variable")and q.execute(variable) etc. etc. many manymore query facilities!

  • 8/6/2019 JDO PRESENTASI

    13/26

    2005-03-31 13

    JDO Queries: More of v1.0 JDOQL Filter can contain almost all Java

    language operators, and some methods such asCollection.isEmpty() & contains(),

    String.startsWith() & endsWith()[But not arbitrary other domain model methods!]

    Ordering

    Free Variables

    Compiled Queries

    Namespaces (packages)

    Navigation (dots) in Filters, Ordering, etc.

  • 8/6/2019 JDO PRESENTASI

    14/26

    2005-03-31 14

    JDO Queries: More in v2.0

    Single-String JDOQL Form

    String.matches(), toLowerCase(), toUpperCase(),indexOf() x2, substring() x2, Map.containsKey() &containsValue(), Math.abs() & sqrt().

    Paging Query Results with Query.setRange() orrange :start to :end and java.util.List results

    User-Defined Result Class, single results, Unique

    Projections, Aggregates functions Standardized SQL pass-through

  • 8/6/2019 JDO PRESENTASI

    15/26

  • 8/6/2019 JDO PRESENTASI

    16/26

    2005-03-31 16

    JDO Implementations[DISCLAIMER]

    Open Source JPOX

    ObjectWeb Speedo

    TJDO

    XORM (?) Apache JDO 2.0 RI (?)

    Orient (ODBMS, OSS is new)

    Commercial JDO ORM Vendors Solarmetric Kodo

    Xcalia (formerly Libelis LiDO) .FR

    Signsoft intellBO .DE Versant Open Access (formerly JDO Genie)

    Others, e.g. Exadel, ObjectFrontier, ObjectMatter, Spadesoft XJDO,

    Commercial non-ORM Versant (incl. Poet)

    ObjectDB (simple &inexpensive)

    Non-JDO ORMs JBoss Hibernate

    Oracle Toplink

    Castor JDO

    OJB (?)

  • 8/6/2019 JDO PRESENTASI

    17/26

    2005-03-31 17

    JDO & EJB2

    .1 EJB 2.1 CMP Entity Beans mixed persistence

    and remoting, with a complicated API and

    development model (container) sorry, butconsider that dead, essentially.

    Session and Message-Driven Beans however

    certainly have their place. So JDO inside theimplementation logic of those makes a lot of

    sense!

  • 8/6/2019 JDO PRESENTASI

    18/26

    2005-03-31 18

    JDO & EJB2

    .1 When using JDO inside a RPC-like (SOA)

    EJB Session Bean (or RMI, Spring

    Remoting.. your-favourite-here) :

    Syntax aspects: About how to get, when

    to etc. For full code details see Core JDO

    book chapter 9 and/or Vendor Doc.

    Architectural aspects: How to write your DTOs

    (AKA VO) here or do you write them at all?!

  • 8/6/2019 JDO PRESENTASI

    19/26

    2005-03-31 19

    JDO & EJB: DTO (A

    KA

    VO) Either, traditionally write dedicated

    SerializableDTO/VO in addition to PC,

    maybe per service/use case. Nothing new;see Core J2EE Patterns etc.

    Or, mark domain model PCs asSerializable

    (although JDO will neverserialize) and use makeTransient (1.0) or

    detachCopy (2.0)

  • 8/6/2019 JDO PRESENTASI

    20/26

    2005-03-31 20

    JDO & EJB: detachCopy New JDO 2.0 state: detached-clean/dirty. API:

    Object PersistenceManager.detachCopy(Object pc);

    Object PersistenceManager.attachCopy(Object detached,boolean makeTransactional);

    Scenario: detach/disconnect, close PM,serialize across tier, modify disconnected,send back. New PM, new Tx, re-attach.

    Keeps original identity and version!

    Simplifies & makes safer the similar manualmakeTransient() approach from Core JDObook chapter 9.

  • 8/6/2019 JDO PRESENTASI

    21/26

    2005-03-31 21

    JDO in the Web Tier (directly) If your project does not need EJB-like remoting and

    physical separation of tiers, directly using JDO inthe Web Tier (Struts, JSF, etc.) can be very nice,

    and lead to a simple and efficient developmentmodel!

    Recommended Architecture: One shared PMF(application context) and one PM and Tx per

    request (context), managed by a Servlet Filter.Better: Enforce Web MVC with Model 2 View,allowing pull view (e.g. JSP) but read-only; allowwrite changes in Controller only.

  • 8/6/2019 JDO PRESENTASI

    22/26

  • 8/6/2019 JDO PRESENTASI

    23/26

    2005-03-31 23

    EJB 3.0 EJB 3.0 EG decided to abandon EJB 2.1

    Entity Beans, and create new and separatePersistence Doc and API:

    This will be an ORM not a transparentPersistence API. Some other differences.However, will also be usable in J2SE,outside J2EE container.

  • 8/6/2019 JDO PRESENTASI

    24/26

    2005-03-31 24

    EJB 3.0

    Also a POJO-style approach. Many realworld domain models will probably be usablewith both APIs!

    Both a similarAPI, conceptually close forpractical purposes, e.g. life cycle aligned.EntityManager.persist(),EntityManager.createQuery(),javax.persistence.Query.getResultList()

    Query Languages (JDOQL & EJBQL) differ,but maybe JDO 2.1 and EJB 3.1 allow cross-spec query language? (A la SQL in JDO 2.0)

  • 8/6/2019 JDO PRESENTASI

    25/26

    2005-03-31 25

    Future of Java Persistence

    Today, the choice is JDO; why wait? A

    proven JCP standard with many

    implementations. EJB 3 Final Release supposedly summer

    2005 (?). Requires JDK 5. Slow adoption?

    Longer Term (2-3 years?) who knows?

    Note: All major JDO implementations very

    likely also EJB 3 "persistence engines".

  • 8/6/2019 JDO PRESENTASI

    26/26

    2005-03-31 26

    Q & A

    Thanks for listening!

    To play with running code after this:

    http://www.jpox.org great tutorials!

    Questions?

    Drinks.