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

Similar documents
CO Java EE 6: Develop Database Applications with JPA

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

Introduction to JPA. Fabio Falcinelli

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

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

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

Index. setmaxresults() method, 169 sorting, 170 SQL DISTINCT query, 171 uniqueresult() method, 169

Web Application Development Using JEE, Enterprise JavaBeans and JPA

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

Java Persistence API (JPA)

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Fast Track to EJB 3.0 and the JPA Using JBoss

Topics in Enterprise Information Management

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

Java Persistence Advanced Concepts

Developing Applications with Java EE 6 on WebLogic Server 12c

Entities are classes that need to be persisted, usually in a relational database. In this chapter we cover the following topics:

The Good, the Bad and the Ugly

JPA The New Enterprise Persistence Standard

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

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

Module 8 The Java Persistence API

High Performance Persistence with EclipseLink

EJB 3 Entity Relationships

Java EE 7: Back-End Server Application Development

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

object/relational persistence What is persistence? 5

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

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

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

EJB 3 Entity Relationships

TopLink Grid: Scaling JPA applications with Coherence

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

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

Courses For Event Java Advanced Summer Training 2018

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

JPA. Java persistence API

Architecture overview

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

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

Introduction to Java Enterprise Edition For Database Application Developer

Java Persistence Query Language (JPQL)

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

Building Java Persistence API Applications with Dali 1.0 Shaun Smith

CO Java EE 7: Back-End Server Application Development

Spring Framework 2.0 New Persistence Features. Thomas Risberg

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

Enterprise JavaBeans, Version 3 (EJB3) Programming

ADVANCED JAVA TRAINING IN BANGALORE

Dynamic Datasource Routing

Schema Null Cannot Be Resolved For Table Jpa

Table of Contents EJB 3.1 and JPA 2

Migrating a Classic Hibernate Application to Use the WebSphere JPA 2.0 Feature Pack

RDBMS Transaction Isolation Levels

U2 Clients and APIs. Java Persistence API User Guide. Version July 2014 UCC-100-JPA-UG-01

Holon Platform JPA Datastore Module - Reference manual. Version 5.2.1

Hibernate Overview. By Khader Shaik

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

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

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

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

Lightweight J2EE Framework

Teneo: Integrating EMF & EclipseLink

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

Introducing EclipseLink: The Eclipse Persistence Services Project

find() method, 178 forclass() method, 162 getcurrentsession(), 16 getexecutablecriteria() method, 162 get() method, 17, 177 getreference() method, 178

Java Enterprise Edition

Enterprise JavaBeans 3.1

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

Monday, November 21, 2011

"Web Age Speaks!" Webinar Series

JPA. Java Persistence API

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Integrating Oracle Coherence 12c (12.2.1)

Prototype 1.0 Specification

Table of Contents. I. Pre-Requisites A. Audience B. Pre-Requisites. II. Introduction A. The Problem B. Overview C. History

Java Persistence API (JPA) Entities

Deployment. See Packaging and deployment processes

<Insert Picture Here> Exploring Java EE 6 The Programming Model Explained

Web Application Development Using Spring, Hibernate and JPA

Web Application Development Using Spring, Hibernate and JPA

Dynamic DAO Implementation

1 Markus Eisele, Insurance - Strategic IT-Architecture

EJB 3.0 Puzzlers. Mike Keith, Oracle Corp. Colorado Software Summit: October 21 26, 2007

Entity LifeCycle Callback Methods Srikanth Technologies Page : 1

JSimpleDB: Language Driven Persistence for Java

Hibernate Tips More than 70 solutions to common Hibernate problems. Thorben Janssen

Enterprise Features & Requirements Analysis For EJB3 JPA & POJO Persistence. CocoBase Pure POJO

Apache OpenJPA User's Guide

1Z Oracle. Java Enterprise Edition 5 Enterprise Architect Certified Master

SUN Sun Certified Enterprise Architect for J2EE 5. Download Full Version :

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

Web Application Development Using Spring, Hibernate and JPA

purequery Deep Dive Part 2: Data Access Development Dan Galvin Galvin Consulting, Inc.

Extracts from Intro to Db - Jdbc - JPA SpringData

Spring Persistence. with Hibernate PAUL TEPPER FISHER BRIAN D. MURPHY

The Object Cache. Table of contents

JVA-163. Enterprise JavaBeans

High Performance Persistence

Metadata driven component development. using Beanlet

Transcription:

Java Persistence API Patrick Linskey EJB Team Lead BEA Systems Oracle plinskey@bea.com

Patrick Linskey EJB Team Lead at BEA JPA 1, 2 EG Member

Agenda JPA Basics What s New ORM Configuration APIs Queries

The Java Persistence API Object / Relational Mapping for Java Direct support for POJOs Powerful query language OLTP-centric Standardized ORM metadata Direct support for Data Transfer Objects Designed for testability Java EE and Java SE

Example Object Model

JPA Usage Example a Session Bean @Stateless @Remote public OrderManagerImpl implements OrderManager { @PersistenceContext private EntityManager em; public Order neworderforproduct(int custid, int prodid) { Customer c = em.find(customer.class, custid); Product p = em.find(product.class, prodid); Order o = new Order(c); em.persist(o); o.addlineitem(new Item(p)); } } return o;

JPQL Syntax Examples // customers 20-30 named Joe, ordered by last name Query q = em.createquery( select c from Customer c where c.firstname = :fname order by c.lastname ); q.setparameter( fname, Joe ); q.setfirstresult(20); q.setmaxresults(10); List<Customer> customers = (List<Customer>) q.getresultlist(); // all orders, as a named query @Entity @NamedQuery(name= Order.findAllOrders, query= select o from Order o ) public class Order {... } Query q = em.createnamedquery( Order.findAllOrders );

Optimistic Locking @Entity public class Order { @Id private long pk; @Version private int oplock;... } Version field can be: numeric temporal (java.util.date, java.sql.date, etc) At commit time, JPA checks that locked records have not changed in the database

Optimistic Lock Types The JPA specification defines two lock types: LockType.READ Asserts that the record has not changed since the data was first read LockType.WRITE Performs a read lock Increments the version field

Optimistic Lock Control Optimistic write locks are automatically obtained for all changed or deleted records Optimistic read locks must be obtained explicitly: em.lock( em.getreference(order.class, 17), LockType.READ) Explicit optimistic write locks to touch records: em.lock( em.getreference(order.class, 17), LockType.WRITE)

getreference() and record deletion Often, you may want to delete the record that corresponds to a given primary key em.delete(em.find(order.class, 17)) 1. SELECT o.id, o.version, o.description FROM Order o WHERE o.id = 17 2. DELETE FROM Order o WHERE o.id = 17 AND o.version =?

getreference() and record deletion getreference() may bypass the lookup that you would see when using find() em.delete(em.getreference(order.class, 17)) 1. DELETE FROM Order o WHERE o.id = 17 DELETE JPQL statements can do this also, but are more opaque to the compiler, since they rely on a string definition language

Disclaimers This is all subject to change I m certainly forgetting some stuff This talk is not expert-group-approved

New Object Model Features

Collections and Maps @Entity, @Embeddable and basic types can be used in Collections and Maps @Entity class PurchaseOrder { @OneToMany(mappedBy= order ) Map<Product,LineItem> lineitems; }

Collections and Maps @Entity, @Embeddable and basic types can be used in Collections and Maps @Entity class MarketingEvent { @ElementCollection Collection<EventType> typecodes; } enum EventType { CUSTOMER, SALES, MARKETING }

Mixed-mode access @Entity @Access(FIELD) class Person { @Id int pk; @Transient String name; @Access(PROPERTY) String getlastname() { return extractlastname(name); } void setlastname(string lastname) {... } }

Related entities as primary keys @IdClass(LineItemPK.class) @Entity class LineItem { @Id int itemnumber; @Id @ManyToOne PurchaseOrder owner; }

@Embeddable classes Can contain most JPA field types @Embeddable @Entity Collections and Maps of basic or @Embeddable types

@OrderColumn Preserves order as defined by the List Different than @OrderBy Position info can be used in JPQL select o from Order o where o.lineitems[3] = :item

New API Methods

Performance Caching EntityManager Working Set 1. cache lookup EntityManagerFactory Performance Cache 3. 2. cache cache population write 2. DB lookup 1. DB write 3. cluster notification Cache Cluster

Performance Cache Maintenance // returned object is never null Cache cache = em.getentitymanagerfactory().getcache(); // cache maintenance after out-of-band updates cache.evict(purchaseorder.class); cache.evict(lineitem.class); // more-targeted maintenance, plus interrogation if (cache.contains(person.class, 17) cache.evict(person.class, 17); // less-targeted maintenance cache.evictall();

Explicit Detachment void EntityManager.clear(T root); Still working on semantic details Detach-in-place vs. detach-a-copy Detach graph size and definition

Data Access Pattern Definition EntityManager.isLoaded( Object entity, String field) EntityManager.load( Object entity, String field)? EntityManager.load( Object entity, AccessPattern ap)? Something via Query?

Programmatic Query Definition Prior Art Hibernate Criteria TopLink ExpressionFactory Quaere (http://quaere.codehaus.org) JPA 2 Expert Group Status Forming a subgroup Hoping to reuse load graph definition

Locking interface EntityManager { enum LockModeType { NONE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT, PESSIMISTIC, PESSIMISTIC_FORCE_INCREMENT } <T> T find(class<t> entitytype, Object pk, LockModeType lockmode); void refresh(object e, LockModeType mode); void lock(object e, LockModeType lockmode); } LockModeType getlockmode(); interface Query { Query setlockmode(lockmodetype type); LockMode getlockmode(); }

Query API Introspection interface Query {... int getfirstresult(); int getmaxresults(); String getname(); QueryType getquerytype(); String getquerystring(); } Set<String> getparameternames(); Object getnamedparameter(string name); Set<String> gethints();

EntityManager API Additions interface EntityManager {... EntityManagerFactory getentitymanagerfactory(); Object getid(object entity); // the properties this EM was configured with Map<String, Object> getproperties(); } // the properties that could have been used // when creating this EM Set<String> getsupportedproperties();

New Configuration Settings

Section Format <persistence> <persistence-unit name= foo > <properties> <property name= new-property value= value /> </properties> </persistence-unit> </persistence> <persistence> <persistence-unit name= foo > <new-setting>value</new-setting> </persistence-unit> </persistence>

Java SE JDBC Configuration jdbc-driver jdbc-url jdbc-user jdbc-password Fully-qualified name of the driver class Driver-specific URL for connecting to the database Username for creating connections Password for creating connections

Performance Cache Configuration enablesecondary-cache logging.show-sql schema.generate Controls whether the EntityManagerFactory cache is enabled for this persistence unit Whether or not to output SQL values might include create force-create update

Things On My List

Named Query Namespace Improvements @NamedQuery(name= findall, query= select p from Person p ) @Entity public class Person {... } @NamedQuery(name= findall, query= select po from PurchaseOrder po ) @Entity public class PurchaseOrder {... } Query q = em.createnamedquery( findall ); Query q = em.createnamedquery(person.class, findall );

Detachment and Lazy Loading Person p = em.find(person.class, 17); Person detached = em.detach(p); // unloaded fields set to Java defaults assert detached.getfriends() == null; // unloaded field access throws exception try { detached.getfriends(); } catch (FieldNotLoadedException e) { // handle exception }

@GeneratedValue on non-pk fields @Entity class Person { @Id @GeneratedValue int pk; @GeneratedValue UUID uuid; }

orm.xml:persistence-unit-defaults Parts of orm.xml belong in persistence.xml cascade-persist entity-listeners

cascade-persist Enabled By Default @Entity class Person { @OneToOne Person spouse; } Person p = new Person( Fred ); p.setspouse(new Person( Wilma ); em.persist(p); => throws an IKnowWhatYouWantedToDoException

Externalization @Entity public class Person {... @Externalizer( toexternalform ) private URL homepage; } This example will automatically use new URL(String) to reverse the process Other options for generating the domain type are available via @Factory annotatio

External Values Lookup tables: a special case of externalization Often useful for supporting enumerated type (or even Java enum types) @Entity public class Magazine { } @ExternalValues({"true=1", "false=2"}) @Type(int.class) private boolean isweekly;

Questions