699994cf3456331bb92b13f3b0a98d4e.ppt
- Количество слайдов: 26
Java Data Objects (JDO) Overview and Future Michael Vorburger, Vertical*i Java User Group Switzerland Zurich, 31. 03. 05 2005 -03 -31
Agenda n n n n Object Persistence O/R Mapping (ORM) History JDO Introduction JDO API JDOQL JDO 2. 0 (JSR 243) n n n Using JDO in EJB 2. 1 Using JDO in Web Apps Using JDO and/or JDBC EJB 3. 0 (JSR 220) Future JDO Implementations (Vendors & OSS) 2005 -03 -31 2
About Speaker n n n 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 2005 -03 -31 3
About Audience n How many of you would consider themselves “moderately familiar” with JDO, or another similar persistence framework, anyway? n How many of you have read the JDO 2. 0 and EJB 3. 0 Persistence Specs? 2005 -03 -31 4
Object Persistence Concepts n Graph of data objects (“Domain Model”) n n API: persist(), Persistence by Reachability Transactions Query! n Internal: Memory Management, Caching, etc. n 2005 -03 -31 5
O/R Mapping (ORM) “Impedance Mismatch” of OOP & RBDMS: n Classes vs. Tables, Inheritance mapping strategies n Relationships: Single 1 -1 or 1 -M, Multiple M-N, “Link with attributes” DBA mixed data and relationship, Inverses/Bi-directionality, Lists, Maps n Identity Management, Uniqueness n Class Attribute vs. Column Type Mappings More in Core JDO Book Chapter 2, but: Solutions for mismatches exist; see also your ORM Vendor Doc! 2005 -03 -31 6
History n n Few things fall from the sky… object persistence in general and JDO in particular build upon a “long” history n Have you heard of any of these names before? n n n 2005 -03 -31 CORBA POS Persistent Object Service Project Forest of Sun Labs (OPJ, PJama) ODMG API Gemstone, O 2, … Top. Link (15 y+) (EJB 2. 1 Entity Beans) 7
JDO Introduction n JDO is an “interface-based API for selection and transformation of transactional persistent storage data into native Java programming language objects” – Transparent Object Persistence! n Various implements for this API exist: Most as ORM for your favourite RDB; some for standalone or builtin OODBMS. n JDO 1. 0 (JSR 12) in 2003 JDO 2. 0 (JSR 243) Extension, accepted 1 m ago n 2005 -03 -31 8
JDO API: PMF & PM n Persistence. Manager. Factory pmf = JDOHelper. get. Persistence. Manager. Factory (Properties p); n javax. jdo. Persistence. Manager pm = pmf. get. Persistence. Manager(); n pm. make. Persistent(customer); n customer = pm. get. Object. By. Id(oid, true); 2005 -03 -31 9
JDO Metadata & Enhancement n n n JDO XML metadata describes which classes can be persisted (<class name=…>), with relevant options (<collection element-type=…>, <extension …>) JDO implementation run-time needs to interact with persistence instances for lifecycle notification, load, store/flush. Generally, this is achieved by running a build-time tool, a JDO [byte code] enhancer. (It adds the Persistence. Capable interface from SPI API to persistent classes. This is no longer strictly for JDO compliance, see Binary. Compatibility. ) Subject of hot debates – strange, think AOP etc. 2005 -03 -31 10
JDO API: Transactions n Non-Managed ¡ javax. jdo. Transaction jtx = pm. current. Transaction(); ¡ jtx. begin(); /* Do Stuff */ jtx. commit(); n Managed (usually in J 2 EE container) ¡ JTA API n n n ¡ n javax. transaction. User. Transaction ut; ut = {lookup somewhere, e. g. JNDI}; ut = ejb. Context. get. User. Transaction() ut. begin(); /* Do Stuff */ ut. commit(); Declarative EJB container managed transactions Core JDO Book Chapter 11 2005 -03 -31 11
JDO API: Query and JDOQL n Query q = pm. new. Query(Customer. class, "customer. Name. starts. With('Tintin') && last. Order. price > 100"); n Collection results = (Collection)q. execute(); Iterator it = results. iterator(); while (it. has. Next()) { Customer cust = (Customer)it. next()); n Parameters: q. declare. Parameters(“String variable") and q. execute(variable) etc. – many more query facilities! 2005 -03 -31 12
JDO Queries: More of v 1. 0 n JDOQL “Filter” can contain almost all Java language operators, and some methods such as Collection. is. Empty() & contains(), String. starts. With() & ends. With() [But not arbitrary other domain model methods!] n Ordering n Free Variables n Compiled Queries n Namespaces (packages) n Navigation (“dots”) in Filters, Ordering, etc. 2005 -03 -31 13
JDO Queries: More in v 2. 0 n n n Single-String JDOQL Form String. matches(), to. Lower. Case(), to. Upper. Case(), index. Of() x 2, substring() x 2, Map. contains. Key() & contains. Value(), Math. abs() & sqrt(). Paging Query Results with Query. set. Range() or “range : start to : end” and java. util. List results User-Defined Result Class, single results, Unique Projections, Aggregates functions Standardized SQL pass-through 2005 -03 -31 14
JDO 2. 0 News n Disconnected Object Graphs for multi-tier n Standardized RDBMS Mapping Descriptors (“just” the agreed upon XML language; many implementations already had possibilities) n Can get e. g. a java. sql. Connection from javax. jdo. Persistence. Manager n More: new. Named. Query, Fetch. Plan, Sequence, delete by query, Multiple User Objects on PM, etc. 2005 -03 -31 15
JDO Implementations [DISCLAIMER] n Open Source ¡ ¡ ¡ JPOX Object. Web Speedo TJDO XORM (? ) Apache JDO 2. 0 RI (? ) Orient (ODBMS, OSS is new) n Commercial non-ORM ¡ ¡ n Non-JDO ORMs ¡ ¡ n Commercial JDO ORM Vendors ¡ ¡ ¡ 2005 -03 -31 Versant (incl. Poet) Object. DB (simple & inexpensive) ¡ JBoss Hibernate Oracle Toplink Castor JDO OJB (? ) … ¡ Solarmetric Kodo ¡ Xcalia (formerly Libelis Li. DO). FR Signsoft intell. BO. DE Versant Open Access (formerly JDO Genie) Others, e. g. Exadel, Object. Frontier, Object. Matter, Spadesoft XJDO, … 16
JDO & EJB 2. 1 n EJB 2. 1 CMP Entity Beans mixed persistence and remoting, with a complicated API and development model (container) – sorry, but consider that dead, essentially. n Session and Message-Driven Beans however certainly have their place. So JDO inside the implementation logic of those makes a lot of sense! 2005 -03 -31 17
JDO & EJB 2. 1 n 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? ! 2005 -03 -31 18
JDO & EJB: DTO (AKA VO) n Either, “traditionally” write dedicated Serializable DTO/VO in addition to PC, maybe per service/use case. Nothing new; see Core J 2 EE Patterns etc. n Or, mark domain model PCs as Serializable (although JDO will never serialize) and use make. Transient (1. 0) or detach. Copy (2. 0) 2005 -03 -31 19
JDO & EJB: detach. Copy n New JDO 2. 0 state: detached-clean/dirty. API: Object Persistence. Manager. detach. Copy(Object pc); Object Persistence. Manager. attach. Copy(Object detached, boolean make. Transactional); n n n 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 manual make. Transient() approach from Core JDO book chapter 9. 2005 -03 -31 20
JDO in the Web Tier (directly) n If your project does not need EJB-like remoting and physical separation of tiers, directly using JDO in the Web Tier (Struts, JSF, etc. ) can be very nice, and lead to a simple and efficient development model! n 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; allow write changes in “Controller“ only. 2005 -03 -31 21
JDO and/or JDBC? n Using JDO (any ORM) does not necessarily mean no JDBC – mixing is possible and may make sense. (Tx safe) [VI LOB] n Core JDO Book chapter 12 has analysis – but outdated already because JDO 2. 0 addresses some cases. n Still, of course, for data-centric, database processing intensive, performance sensitive e. g. batch kind applications, as well as for very complex existing fixed RDB schemas, ORM and thus JDO may not be the appropriate road to go. 2005 -03 -31 22
EJB 3. 0 n EJB 3. 0 EG decided to abandon EJB 2. 1 Entity Beans, and create new and separate Persistence Doc and API: n This will be an ORM not a transparent Persistence API. Some other differences. However, will also be usable in J 2 SE, outside J 2 EE container. 2005 -03 -31 23
EJB 3. 0 n n Also a POJO-style approach. Many real world domain models will probably be usable with both APIs! Both a similar API, conceptually close for practical purposes, e. g. life cycle aligned. Entity. Manager. persist(), Entity. Manager. create. Query(), javax. persistence. Query. get. Result. List() n Query Languages (JDOQL & EJBQL) differ, but maybe JDO 2. 1 and EJB 3. 1 allow crossspec query language? (A la SQL in JDO 2. 0) 2005 -03 -31 24
Future of Java Persistence n n 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". 2005 -03 -31 25
Q&A n Thanks for listening! n To play with “running code” after this: http: //www. jpox. org – great tutorials! n Questions? n Drinks. 2005 -03 -31 26
699994cf3456331bb92b13f3b0a98d4e.ppt