The Good, the Bad and the Ugly

Similar documents
Shale and the Java Persistence Architecture. Craig McClanahan Gary Van Matre. ApacheCon US 2006 Austin, TX

EJB 3 Entity Relationships

EJB 3 Entity Relationships

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Fast Track to EJB 3.0 and the JPA Using JBoss

Enterprise JavaBeans, Version 3 (EJB3) Programming

Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

Java EE Architecture, Part Three. Java EE architecture, part three 1(69)

object/relational persistence What is persistence? 5

What data persistence means? We manipulate data (represented as object state) that need to be stored

Java Persistence API (JPA)

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

JPA The New Enterprise Persistence Standard

Java EE 7: Back-End Server Application Development

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

CO Java EE 6: Develop Database Applications with JPA

JPA and CDI JPA and EJB

5/2/2017. The entity manager. The entity manager. Entities lifecycle and manipulation

Introduction to JPA. Fabio Falcinelli

Chapter 1 Introducing EJB 1. What is Java EE Introduction to EJB...5 Need of EJB...6 Types of Enterprise Beans...7

Building Java Persistence API Applications with Dali 1.0 Shaun Smith

SUN Sun Cert Bus Component Developer Java EE Platform 5, Upgrade. Download Full Version :

Module 8 The Java Persistence API

Topics in Enterprise Information Management

Lightweight J2EE Framework

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

WHAT IS EJB. Security. life cycle management.

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

1Z Oracle. Java Enterprise Edition 5 Business Component Developer Certified Professional Upgrade

Java Persistence API. Patrick Linskey EJB Team Lead BEA Systems Oracle

Java EE Architecture, Part Three. Java EE architecture, part three 1(24)

Deployment. See Packaging and deployment processes

Spring Framework 2.0 New Persistence Features. Thomas Risberg

Session 13. Reading. A/SettingUpJPA.htm JPA Best Practices

Database Connection using JPA. Working with the Java Persistence API (JPA) consists of using the following interfaces:

Deccansoft Software Services. J2EE Syllabus

<Insert Picture Here> Productive JavaEE 5.0 Development

Enterprise JavaBeans 3.1

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13

CO Java EE 7: Back-End Server Application Development

Courses For Event Java Advanced Summer Training 2018

"Charting the Course... Mastering EJB 3.0 Applications. Course Summary

Table of Contents EJB 3.1 and JPA 2

Practice Test. Oracle 1z Java Enterprise Edition 5 Business Component Developer Certified Professional Upgrade Exam. Version: 14.

ADVANCED JAVA TRAINING IN BANGALORE

Object-relational mapping EJB and Hibernate

TopLink Grid: Scaling JPA applications with Coherence

EJB 3 Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

Seam 3. Pete Muir JBoss, a Division of Red Hat

Introduction. Enterprise Java Instructor: Please introduce yourself Name Experience in Java Enterprise Edition Goals you hope to achieve

Refactoring to Seam. NetBeans. Brian Leonard Sun Microsystems, Inc. 14o

Developing Applications with Java EE 6 on WebLogic Server 12c

Polyglot Persistence. EclipseLink JPA for NoSQL, Relational, and Beyond. Shaun Smith Gunnar Wagenknecht

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

POJOs to the rescue. Easier and faster development with POJOs and lightweight frameworks

Introduction to Seam. Pete Muir. JBoss, a division of Red Hat

Hibernate Change Schema Name Runtime

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

JPA Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

Java Enterprise Edition

How to Develop a Simple Crud Application Using Ejb3 and Web Dynpro

Developing Java EE 5 Applications from Scratch

Web Application Development Using Spring, Hibernate and JPA

Dynamic Datasource Routing

Injection Of Entitymanager

V3 EJB Test One Pager

Leverage Rational Application Developer v8 to develop Java EE6 application and test with WebSphere Application Server v8

Java EE 6: Develop Business Components with JMS & EJBs

Seam. Pete Muir JBoss, a Division of Red Hat.

11-15 DECEMBER ANTWERP BELGIUM

Enterprise JavaBeans. Layer:08. Persistence

NetBeans IDE Field Guide

Oracle EXAM - 1Z Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam. Buy Full Product.

A COMPONENT BASED ONLINE ORDERING SYSTEM USING ENTERPRISE JAVABEANS 3.0

Spring Framework 2.5: New and Notable. Ben Alex, Principal Software Engineer, SpringSource

JVA-117A. Spring-MVC Web Applications

Oracle - Developing Applications for the Java EE 7 Platform Ed 1 (Training On Demand)

Introducing EclipseLink: The Eclipse Persistence Services Project

Author: Chen, Nan Date: Feb 18, 2010

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

Migrating traditional Java EE applications to mobile

Exam Questions 1Z0-895

JPA. Java Persistence API

Implementing a Web Service p. 110 Implementing a Web Service Client p. 114 Summary p. 117 Introduction to Entity Beans p. 119 Persistence Concepts p.

Using JNDI from J2EE components

Comparative Analysis of EJB3 and Spring Framework

New Features in EJB 3.1

Introduction to Java Enterprise Edition For Database Application Developer

Overview p. 1 Server-side Component Architectures p. 3 The Need for a Server-Side Component Architecture p. 4 Server-Side Component Architecture

Oracle Exam 1z0-898 Java EE 6 Java Persistence API Developer Certified Expert Exam Version: 8.0 [ Total Questions: 33 ]

Web Application Development Using Spring, Hibernate and JPA

Setting Schema Name For Native Queries In. Hibernate >>>CLICK HERE<<<

Web Application Development Using Spring, Hibernate and JPA

Hibernate Interview Questions

J2EE - Version: 25. Developing Enterprise Applications with J2EE Enterprise Technologies

Oracle Fusion Middleware

WAS V7 Application Development

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

Introduction to CDI Contexts and Dependency Injection

Transcription:

The Good, the Bad and the Ugly 2 years with Java Persistence API Björn Beskow bjorn.beskow@callistaenterprise.se www.callistaenterprise.se

Agenda The Good Wow! Transparency! The Bad Not that transparent after all The Ugly JPA Deployment model and JavaEE integration What s next? 2 years with JPA, Slide 2

JPA 1.0 Industry Standard Object Relational Mapping framework Declarative Mapping Framework API Query Language Transaction support 2 years with JPA, Slide 3

JPA Key Concepts 2 years with JPA, Slide 4

Key Concepts: Entity - Plain Old Java Object with persistent identity - Declarative mapping between object and database schema -Queryable via Java Persistence Query Language (JPQL) - Managed at runtime via EntityManager 2 years with JPA, Slide 5

Key Concepts: Entity Manager - API for managing entity lifecycle - persisting, refreshing, merging and removing entities - API for finding entities and create entity queries - find, createquery, createnamedquery 2 years with JPA, Slide 6

Key Concept: Persistence Context - Runtime context for an Entity Manager - Contains a set of managed entities - Lifecycle may be managed - by application or - by JavaEE container - Lifetime may be - Bound to Tx scope - Extended 2 years with JPA, Slide 7

Application Managed Persistence Context - The application manages the Entity Manager, i.e. is responsible for - Creating and closing the Entity Manager (and its related Persistence Context) - Demarcate transactions - Propagate the Entity Manager to all interested parties - May relate the lifecycle of the Entity Manager to transaction boundaries, or extend it across TX boundaries - Typically used in a Java SE 5.0 environment 2 years with JPA, Slide 8

Container-Managed Persistence Context - A JavaEE container manages the Entity Manager and its related Persistence Context - Comes in two flavors 1. Transaction-Scoped Persistence Context - Automatically created when a transaction is started - Automatically propagated to all components that participate in that transaction - Automatically closed at transaction demarcation 2. Extended Persistence Context - Governed by the lifecycle of a Stateful Session Bean - May span multiple transactions 2 years with JPA, Slide 9

State Model for JPA Entities 2 years with JPA, Slide 10

Key Concept: EntityManagerFactory - Factory API for EntityManagers - Configured at Bootstrap/Deployment time 2 years with JPA, Slide 11

Key Concept: PersistenceUnit - Unit of Persistence Packaging and Deployment - Configuration for an EntityManagerFactory - Object Relational Mapping Info - Annotation-based or DD based - Maps into a single database 2 years with JPA, Slide 12

Example Entity @Entity public class Order implements Serializable { @Id @GeneratedValue private Long orderid; private double price; private Date estimateddelivery; } 2 years with JPA, Slide 13

Example EntityManager usage @Stateless public class OrderServicesBean implements OrderServices { @PersistenceContext(unitName = "OrderPU") private EntityManager entitymanager; public void processorder(long orderid) { } } Order o = entitymanager.find(orderid, Order.class);... o.setprice(calculateprice(o.getcustomer()); e.setestimateddelivery(erp.delivery);... // No explicit call to save changes // happens automatically (eventually) 2 years with JPA, Slide 14

The Good: POJO based, transparent persistence 1. Entities are unaware of their Persistency they are pure POJO s (Plain Old Java Objects) 3. The JPA provider keeps the entities in synch with the database 2. Non entity classes uses the Entity Manager to control persistency 2 years with JPA, Slide 15

No more Embedded CRUD SQL Connection con = datasource.getconnection(); PreparedStatement stmt = null; try { stmt = con.preparestatement("update products SET price =?"); stmt.setint(1, 200); stmt.executeupdate(); } finally { if (stmt!= null) { try { stmt.close(); } catch (SQLException ex) { logger.warn("could not close statement", ex); } } try { con.close(); } catch (SQLException ex) { logger.warn("could not close connection", ex); } } 2 years with JPA, Slide 16

No more DAOs public interface CustomerDao { public void createcustomer(customerdto customer) public CustomerDOT retrievecustomer(string ssn) throws UnknownCustomerException; public CustomerDTO updatecustomer(customerdto customer) throws UnknownCustomerException; } public void deletecustomer(string ssn) throws UnknownCustomerException; 2 years with JPA, Slide 17

No more DTOs public class CustomerDTO { private String ssn; private String name; } public String getssn() { return ssn; } public void setssn(string ssn) { this.ssn = ssn; } public String getname() { return name; } public void setname(string name) { this.name = name; } 2 years with JPA, Slide 18

Managing Relationships Expressed in Mapping Metadata as well Allows object graphs to be navigated much more convenient than explicit Joins! 2 years with JPA, Slide 19

Managing Relationships: Mapping @Entity public class Customer implements Serializable { @OneToMany(mappedBy = "customer") private List<Order> orders; } 2 years with JPA, Slide 20

Managing Relationships: Mapping (contd) @Entity public class Order implements Serializable { @ManyToOne private Customer customer; } @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) private List<OrderItem> orderitems; 2 years with JPA, Slide 21

Managing Relationships: Mapping (contd) @Entity public class OrderItem implements Serializable { @ManyToOne private Order order; } 2 years with JPA, Slide 22

Relationships Lazy and Eager loading Controlling when related entities are loaded Only loaded when navigated to Eager Load related entities when the parent entity is loaded Lazy Load related entities if/when they are navigated to Loaded when its parent Order is loaded 2 years with JPA, Slide 23

Example Lazy Loading scenario Loading of Customer delayed until first requested 2 years with JPA, Slide 24

Relationships Lazy and Eager loading Potential performance problem: Too many small SQL statements Potential performance problem: Too large SQL statements 2 years with JPA, Slide 25

Static vs. Dynamic Lazy/Eager Loading Eager Loading is the default for OneToOne relations, whereas Lazy Loading is the default for OneToMany and ManyToMany relations Can be statically overridden using annotation attributes or DD metadata: @Entity public class Order implements Serializable { @ManyToOne private Customer customer; } @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) private List<OrderItem> orderitems; JPQL Queries can be used to dynamically change Lazy into Eager loading: SELECT o FROM Order o JOIN FETCH o.customer WHERE... 2 years with JPA, Slide 26

Experiences from the trenches Excellent developer productivity! Order of magnitude more productive than JDBC Much shorter start-up time compared to previous, model-based code generation approach Simpler, less error-prone development tools Portability across JPA vendors and DB vendors Adequate expressive power and flexibility of Mapping mechanism Handles most cases Performance equal to or better than previous JDBC-based framework Need to fine-tune balance between Lazy versus Eager loading 2 years with JPA, Slide 27

The Bad Not that transparent in reality Lifecycle of Persistence Context governs detachment, which in turn affects lazy loading updates and merges Not that simple to foresee and comprehend! 2 years with JPA, Slide 28

Lazy Loading and Detachment Lazy loaded relations are fetched transparently when needed, as long as the entity is managed. When an entity has become detached, any lazy loaded relations must not be accessed. A lazy load problem occurs when trying to retrieve a lazily loaded related entity from a detached entity, i.e. when the persistence context is already closed. Lazy load problems are highly elusive and difficult to guard against! Experience from the trenches: >75% of reported JPA-related defects due to Lazy Load problems 2 years with JPA, Slide 29

Typical Web Request-Response sequence Scenario: Web application + Container Managed and Transaction-Scoped Persistence Context 2 years with JPA, Slide 30

Typical Web Request-Response sequence Lazy Loading TX Boundary This will result in a Lazy Load problem 2 years with JPA, Slide 31

Preparing for Detachment Since a Transaction-Scoped Persistence Context is automatically closed at the transaction boundary, any entities that are passed out of the transaction boundary must be prepared for detachment: All relations and/or attributes that is of interest to the consumer of the entity must be already loaded. Can be achieved by accessing the relation programmatically (i.e. calling the getter): public Order getorder(long orderid) { } Order o = entitymanager.find(orderit, Order.class);... o.getcustomer(); return o; Or by using a fetch join JPQL query 2 years with JPA, Slide 32

Preparing for Detachment will most likely affect your Service interface public interface OrderServices { } public Order getorder(); public Order getorderwithcustomer(); public Order getorderwithorderitems(); public Order getorderwithcustomerandorderitems(); public Order getorderwithorderitemsandarticles();... 2 years with JPA, Slide 33

Detecting Lazy Load problems The JPA 1.0 specification does not clearly state how a lazy load problem should be handled and signalled by the JPA implementation Several different behavious have be observed between JPA providers: No lazy problem occurs, because the JPA implementation implements the lazy relationship eagerly No lazy problem occurs, because the JPA implementation fetches the related entities from the database even though the entity manager is closed A vendor-specific exception is thrown 2 years with JPA, Slide 34

Updating Entities in a Stateless Web setting Scenario: Web application + Container Managed and Transaction-Scoped Persistence Context 2 years with JPA, Slide 35

Naive Approach: Merge new entity HTTP Request/Response boundary 2 years with JPA, Slide 36

Naive Approach: Sample code Transactional boundary Application Logic (e.g. Web) Business Logic Order o = new Order(); o.setorderno(orderno) o.setversion(version); o.setestimateddelivery(delivery);... o = service.updateorder(o); public Order updateorder(order o) { return em.merge(o); } Requires setting of all attributes Poorly documented behavior in spec You re up for a nasty surprise! 2 years with JPA, Slide 37

Remember the difference between New and Detached Entities? 2 years with JPA, Slide 38

A subtle difference between 2 years with JPA, Slide 39

... and 2 years with JPA, Slide 40

Alternative #1: update and merge detached entity HTTP Request/Response boundary 2 years with JPA, Slide 41

Alternative #1: Sample code Transactional boundary Application Logic (e.g. Web) Business Logic Order o = (Order)session.getAttribute("Order"); o.setestimateddelivery(delivery); o = service.updateorder(o); public Order updateorder(order o) { return em.merge(o); } Requires explicit use of the HTTP Session HTTP Session must be correctly initialized from the previous page The detached entity must be removed explicit from the HTTP Session How large can a serialized detached entity become? 2 years with JPA, Slide 42

Alternative #2: Re-read and update 2 years with JPA, Slide 43

Alternative #2: Sample code Transactional boundary Application Logic (e.g. Web) Business Logic Order o = new Order(); o.setorderno(orderno) o.setestimateddelivery(delivery);... o = service.updateorder(o); public Order updateorder(order o) { Order o = service.find(o.getorderno(), Order.class); o.setestimateddelivery(delivery); return o; } Requires extra SQL SELECT round-trip Requires setting of all attributes in both application and business logic 2 years with JPA, Slide 44

The Ugly The rigid JPA Deployment and Packaging model prevents testability Does not blend well with JavaEE Naming and separation of concerns between Developer and Deployer 2 years with JPA, Slide 45

Persistence Archives and Persistence Units Unit of Persistence Packaging and Deployment Configuration for an EntityManagerFactory Object Relational Mapping Info Annotation-based or Deployment Descriptor based Persistence ARchive: JAR archive containing a persistence.xml file placed in the META-INF folder 2 years with JPA, Slide 46

Persistence.xml and Pluggability The persistence.xml deployment descriptor specifies physical information, which cannot easily be changed in different contexts (e.g. in Unit Tests) <persistence-unit name="orderpu" transaction-type="jta"> <persistence-unit name="orderpu" transaction-type="resource_local">...... <jta-data-source>jdbc/jee5testdb</jta-data-source> <properties>...... <properties> <property name="hibernate.connection.driver_class"... value="org.apache.derby.jdbc.clientdriver" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.connection.url"... value="jdbc:derby://localhost:1527/jee5testdb_hb" /> </properties> <property name="hibernate.connection.username" </persistence-unit> value="app_hb" /> <property name="hibernate.connection.password" value="app" /> <property name="hibernate.hbm2ddl.auto" value="update" />... </properties> </persistence-unit> 2 years with JPA, Slide 47

Custom Framework Solutions Gap filled by additional frameworks Spring provides a custom JPA bootstrapping mechanism to allow configuration of Persistence Units Current project solution: Specify multiple PersistenceUnits, choose which one to use based on runtime configuration <!-- In-container persistence unit --> <persistence-unit name="orderpu" transaction-type="jta">... </persistence-unit> <!-- Out-of-container Tests persistence unit --> <persistence-unit name="orderpu_test" transaction-type="resource_local">... </persistence-unit> 2 years with JPA, Slide 48

What does <jta-datasource> mean? <persistence-unit name="pospu" transaction-type="jta">... <jta-data-source>jdbc/posdb</jta-data-source>... </persistence-unit> JNDI lookup string for Datasource? It s not specified! 2 years with JPA, Slide 49

Naming: JavaEE Component Scope JavaEE requires the app server to support a logical naming tree visible to components of the same enterprise application Logical names are referenced through a standardized virtual sub-context: java:comp/env The deployer maps the logical name to an external name visible to all clients of the network 2 years with JPA, Slide 50

Hence we should use a logical name in persistence.xml as well? <persistence-unit name="pospu" transaction-type="jta">... <jta-data-source>java:comp/env/jdbc/posdb</jta-data-source>... </persistence-unit> But where should we place the resource ref? The JPA entities are not JavaEE Components, hence they have no associated Component Scope No standardized Application Scope exists (even though some App Server vendors allow configuration of resources in their proprietary deployment descriptors) On one of the components that uses the Persistence Unit? 2 years with JPA, Slide 51

Using the @Resource attribute @Resource(name="jdbc/PosDB", mappedname="jdbc/posdb_v1") @Stateless public class OrderServicesBean implements OrderServices { private EntityManager em = null; } @PersistenceContext(unitName="PosPU") public void setentitymanager(entitymanager em) { this.em = em; } 2 years with JPA, Slide 52

But what if...? @Resource(name="jdbc/PosDB", mappedname="jdbc/posdb_v2") @Stateless public class CustomerServicesBean implements CustomerServices { private EntityManager em = null; } @PersistenceContext(unitName="PosPU") public void setentitymanager(entitymanager em) { this.em = em; } 2 years with JPA, Slide 53

And besides, it doesn t work Currently no portable way exist to use a logical JNDI name in Persistence.xml Requires build-time manipulation of EAR and EJB-JAR files to provide physical details 2 years with JPA, Slide 54

To conclude JPA s Transparent Persistency is a giant step forward Powerful Good productivity But as (probably) any abstraction, JPA tends to leak Understanding the Persistence Context is critical May rapidly affect the productivity equation Still immature in some aspects Issues with configuration pluggability (e.g. for testing) Issues with JavaEE integration Looking ahead 2 years with JPA, Slide 55

JPA 2.0 JSR 317 Expert Group formed in mid 2007 under the lead of Linda DeMichiel First Public Review scheduled for Q2 2008 Final Release scheduled for Q4 2008, with Reference Implementation late Q2 2009 2 years with JPA, Slide 56

JPA 2.0 Scope Expanded object/relational mapping functionality Additions to the Java Persistence query language An API for "criteria" queries Standardization of sets of "hints" for query configuration and for entity manager configuration Standardization of additional metadata to support DDL generation Expanded pluggability contracts to support efficient passivation and replication of extended persistence contexts in Java EE environments Standardization of additional contracts for entity detachment and merge, and persistence context management Better support for validation 2 years with JPA, Slide 57

Time (?) for Questions! 2 years with JPA, Slide 58