efficient object relational mapping for java and j2ee
play

Efficient Object-Relational Mapping for JAVA and J2EE Applications - PowerPoint PPT Presentation

Efficient Object-Relational Mapping for JAVA and J2EE Applications or the impact of J2EE on RDB Marc Stampfli Oracle Software (Switzerland) Ltd. According to customers about Underestimation 20-50% percent of the time of developer is used


  1. Efficient Object-Relational Mapping for JAVA and J2EE Applications – or the impact of J2EE on RDB Marc Stampfli Oracle Software (Switzerland) Ltd.

  2. According to customers about Underestimation 20-50% percent of the time of developer is used for manual object-relational mapping . Managing persistence related issues is the most underestimated challenge in enterprise Java today – in terms of complexity, effort and maintenance Doesn ’ t JDBC handle these issues?

  3. Agenda � Impedance Mismatch � Object Persistence Options � J2EE Persistence Requirements � J2EE Persistence Framework

  4. Focus of attention Enterprise App. Architecture Distribution Server Content Server, Web Server,

  5. JDBC � Java standard for accessing databases � JDBC is simply the JDBC database connection rows SQL utilities Java developers need to build upon • Connection con = DriverManager.getConnection(…); • Statement stmt = con.createStatement(); • stmt.execute(" create table JUGSData ("+ "programmer varchar (32),"+"day char (3),"+”cups integer); ")

  6. SQLJ � Meta-Standard for accessing databases � Pre-compiler to build SQLJ Pre- Compiler JDBC Java Code JDBC rows SQL 1. #sql iterator Iter (double sal, String ename); 2. String ename = 'Smith'; 3. Iter it; ... 4. #sql it = { select ENAME, SAL from EMP where ENAME = :ename };

  7. Impedance Mismatch � The differences in relational and object technology is known as the “object-relational impedance mismatch” � Challenging problem to address because it requires a combination of relational database and object expertise

  8. Impedance Mismatch Factor J2EE Relational Databases Logical Data Objects, methods, Tables, SQL, stored procedures Representation inheritance Scale Hundreds of megabytes Gigabytes, terabytes Relationships Memory references Foreign keys Uniqueness Internal object id Primary keys Key Skills Java development, SQL, Stored Procedures, data object modeling management Tools IDE, Source code Schema designer, query management, Object manager, performance profilers, Modeler database configuration

  9. Object Level Options � Depends on what component architecture is used: – Entity Beans BMP – Bean Managed Persistence – Entity Beans CMP – Container Managed Persistence – Access Java Objects via Persistence Layer (POJO or J2EE) Do you build � Can be off the shelf or “home-grown” your O-R Mapping Tool yourself?

  10. Entity Beans - BMP � In BMP, developers write the persistence code themselves � Database reads and writes occur in specific methods defined for bean instances � The container calls these methods - usually on method or transaction boundaries ejbLoad() - “load yourself” ejbStore() - “store yourself” ejbCreate() - “create yourself” findBy…() - “find yourself” ejbRemove() - “remove yourself”

  11. Entity Beans - CMP � Persistence is based on information in the deployment descriptors More “automatic” persistence – managed by the Application – Server, can be faster than BMP No special persistence code in the bean – Description of the persistence done with tools and XML files – � Less control, persistence capabilities are limited to the functionality provided. Very difficult to customize or extend CMP features as it is – built-in Do have options to plug-in a 3 rd party CMP solution on an – app server

  12. Object Persistence Layer � Abstracts persistence details from the application layer, supports Java objects/Entity Beans J2EE & J2EE & J2EE & J2EE & Web Web Web Web Services Services Services Services object-level object creation and Objects querying and creation updates through Objects results are objects object-level API Persistence Layer JDBC results are API uses SQL rows SQL returned as or database raw data specific calls

  13. Basic J2EE Persistence Checklist � Mappings � Object traversal � Queries � Transactions � Optimized database interaction � Database Triggers and Cascade Deletes � Caching � Locking � Database features

  14. Mapping � Object model and Schema must be mapped – True for any persistence approach � Most contentious issue facing designers – Which classes map to which table(s)? – How are relationships mapped? – What data transformations are required?

  15. Good and Poor Mapping Support � Good mapping support: Domain classes don’t have to be “tables” – References should be to objects, not foreign keys – Database changes (schema and version) easily handled – � Poor mapping support: Classes must exactly mirror tables – Middle tier needs to explicitly manage foreign keys – Classes are disjoint – Change in schema requires extensive application changes –

  16. Data and Object Models � Rich, flexible mapping capabilities provide data and object models a degree of independence � Otherwise, business object model will force changes to the data schema or vice-versa � Often, J2EE component models are nothing more than mirror images of data model – NOT desirable

  17. Simple Object Model Customer id: int name: String creditRating: int 1:1 Relationship Address id: int city: String zip: String

  18. Typical 1-1 Relationship Schema ZIP ADDR CITY ID A_ID C_RATING CUST NAME ID

  19. Other possible Schemas… ADDR CUST ID CITY ZIP C_ID ID NAME C_RATING CUST ADDR CUST_ADDR ID NAME C_RATE ID CITY ZIP C_ID C_ID A_ID CUST ID NAME C_RATING CITY ZIP

  20. Even More Schemas… CUST CUST_CREDIT CUST CUST_CREDIT ID NAME A_ID ID C_RATING ID NAME ID C_RATING ADDR ADDR ID CITY ZIP ID CITY ZIP C_ID CUST CUST_CREDIT ID NAME ID C_RATING A_ID CUST CUST_CREDIT ID NAME ID C_RATING ADDR ID CITY ZIP ADDR ID CITY ZIP C_ID CUST CUST_CREDIT ID NAME CC_ID ID C_RATING A_ID ADDR ID CITY ZIP

  21. Mapping Summary � Just showed nine valid ways a 1-1 relationship could be represented in a database – Most persistence layers and application servers will only support one � Without good support, designs will be forced � Imagine the flexibility needed for other mappings like 1-M and M-M

  22. Object Traversal – Lazy Reads � J2EE applications work on the scale of a few hundreds of megabytes � Relational databases routinely manage gigabytes and terabytes of data � Persistence layer must be able to transparently fetch data “just in time”

  23. Just in Time Reading – Faulting Process 1. Accessing relationship for first time Customer Proxy 2. Get related object based on 4. Plug FK result 3b. SQL if into not cached Proxy 3a. Check Cache Order Order

  24. Object Traversals � Even with lazy reads, object traversal is not always ideal – To find a phone number for the manufacturer of a product that a particular customer bought, may do several queries: � Get customer in question � Get orders for customer � Get parts for order � Get manufacturer for part � Get address for manufacturer – Very natural object traversal results in 5 queries to get data that can be done in 1

  25. N+1 Reads Problem � Many persistence layers and application servers have an N+1 reads problem � Causes N subsequent queries to fetch related data when a collection is queried for � A side effect of the impedance mismatch and poor mapping and querying support in persistence layers

  26. N+1 Reads � Must have solution to minimize queries � Need flexibility to reduce to 1 query, 1+1 query or N+1 query where appropriate – 1 Query when displaying list of customers and addresses – known as a “Join Read” – 1+1 Query when displaying list of customers and user may click button to see addresses – known as a “Batch Read” – N+1 Query when displaying list of customers but only want to see address for selected customer

  27. Queries � Java developers are not usually SQL experts – Maintenance and portability become a concern when schema details hard-coded in application � Allow Java based queries that are translated to SQL and leverage database options – EJB QL, object-based proprietary queries, query by example

  28. Queries � Persistence layer handles object queries and converts to SQL � SQL issued should be as efficient as written by hand � Should utilize other features to optimize Parameter binding, cached statements – � Some benefits to dynamically generated SQL : Ability to create minimal update statements – � Only save objects and fields that are changed Simple query-by-example capabilities –

  29. Query Requirements � Must be able to trace and tune SQL � Must be able use ad hoc SQL where necessary � Must be able to leverage database abilities – Outer joins – Nested queries – Stored Procedures – Oracle Hints

  30. Transaction Management � J2EE apps typically support many clients sharing small number of db connections � Ideally would like to minimize length of transaction on database Begin Txn Time Begin Txn Commit Txn Commit Txn

  31. Caching � Any application that caches data, now has to deal with stale data � When and how to refresh? � Will constant refreshing overload the database? � Problem is compounded in a clustered environment � App server may want be notified of database changes

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend