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

Similar documents
Developing Applications with Java EE 6 on WebLogic Server 12c

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

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

The Good, the Bad and the Ugly

object/relational persistence What is persistence? 5

JPA and CDI JPA and EJB

Java Enterprise Edition

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

JPA The New Enterprise Persistence Standard

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

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

Introduction to JPA. Fabio Falcinelli

Java EE 7: Back-End Server Application Development

Seam & Web Beans. Pete Muir JBoss, a division of Red Hat.

"Web Age Speaks!" Webinar Series

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

<Insert Picture Here> Productive JavaEE 5.0 Development

Java EE 6: Develop Business Components with JMS & EJBs

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

Spring Framework 2.0 New Persistence Features. Thomas Risberg

Java SE7 Fundamentals

Fast Track to EJB 3.0 and the JPA Using JBoss

Courses For Event Java Advanced Summer Training 2018

Distributed Multitiered Application

Migrating traditional Java EE applications to mobile

TheServerSide.com. Dependency Injection in Java EE 6 - Part 5

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Sun Java Studio Creator. Ken Paulsen Staff Engineer Sun Microsystems, Incorporated (Slides by: Craig R. McClanahan)

JVA-163. Enterprise JavaBeans

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Module 8 The Java Persistence API

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

JSR 299: Web Beans. Web Beans Expert Group. Version: Public Review

Enterprise JavaBeans, Version 3 (EJB3) Programming

Metadata driven component development. using Beanlet

ADVANCED JAVA TRAINING IN BANGALORE

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

CO Java EE 6: Develop Database Applications with JPA

Improve and Expand JavaServer Faces Technology with JBoss Seam

Entity LifeCycle Callback Methods Srikanth Technologies Page : 1

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

INTRODUCTION TO COMPONENT DESIGN IN JAVA EE COMPONENT VS. OBJECT, JAVA EE JAVA EE DEMO. Tomas Cerny, Software Engineering, FEE, CTU in Prague,

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

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

Injection Of Entitymanager

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

This is the first part of a multi-article series. For part 2 please see: Dependency Injection in Java EE 6 - Part 2

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

Java Advance Frameworks

Java EE 6 - Update Harpreet Singh GlassFish Portfolio Product Manager

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

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

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

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

CO Java EE 7: Back-End Server Application Development

Lightweight J2EE Framework

Java EE 6: Develop Web Applications with JSF

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

Chapter 6 Enterprise Java Beans

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

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

Contents at a Glance

EJB ENTERPRISE JAVA BEANS INTRODUCTION TO ENTERPRISE JAVA BEANS, JAVA'S SERVER SIDE COMPONENT TECHNOLOGY. EJB Enterprise Java

JBoss World 2009 Marius Bogoevici

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

Topics in Enterprise Information Management

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

Hands-on Development of Web Applications with Java EE 6

WHAT IS EJB. Security. life cycle management.

Deccansoft Software Services. J2EE Syllabus

Fast Track to Java EE

Enterprise JavaBeans 3.1

Oracle Fusion Middleware 11g: Build Applications with ADF I

Apache OpenWebBeans and DeltaSpike Deep Dive Mark Struberg Gerhard Petracek

Apache TomEE Tomcat with a kick

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

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

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

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

Introduction to Spring 5, Spring MVC and Spring REST

Oracle Fusion Middleware 11g: Build Applications with ADF I

Practical EJB 3.0. Bill Burke JBoss Fellow Red Hat. Easier for application and framework developers. Professional Open Source

Building Web Applications With The Struts Framework

PATTERNS & BEST PRACTICES FOR CDI

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

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

}w!"#$%&'()+,-./012345<ya

Oracle Developer Day

Vision of J2EE. Why J2EE? Need for. J2EE Suite. J2EE Based Distributed Application Architecture Overview. Umair Javed 1

1 Markus Eisele, Insurance - Strategic IT-Architecture

What is it? What does it do?

EJB 3 Entity Relationships

Java SE 8 Fundamentals

Business Logic and Spring Framework

/ / JAVA TRAINING

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

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

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

NetBeans IDE Field Guide

Author: Chen, Nan Date: Feb 18, 2010

Transcription:

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

Agenda The Apache Shale Framework Java Persistence Architecture Design Patterns for Combining Frameworks Questions and Answers 2

The Apache Shale Framework Architected as extensions to the JavaServer Faces controller framework Key functional components: View controller (application callbacks) Dialog manager (scoped conversations) Clay plug-in (alternate view handler) Tiger extensions (annotations based config) Remoting (AJAX back end support) Struts compatibility functionality: Tiles, Validator, Token 3

View Controller For our purposes, View Controller is the key touchpoint between the frameworks Based on JSF convention of backing beans Four application oriented callbacks: init() -- called when view is created or restored preprocess() -- called when about to process a postback prerender() -- called when about to render this view destroy() -- called after rendering, if init() was called Also supports init/destroy lifecycle events for request/session/application scoped data beans 4

View Controller and Model Tier Questions about backing beans (and view controllers): Where is the business logic? How is the model tier accessed? We will look into options after we explore JPA... 5

Java Persistence Architecture Part of JSR-220 (Enterprise JavaBeans 3.0) Began as a simplification to entity beans Evolved into POJO based persistence technology: Rich modelling capabilities, inheritance, polymorphism Standardized object/relational mapping Powerful query capabilities Scope expanded at the request of the community: Into persistence technology for Java EE To support out-of-container use is Java SE To support pluggable persistence providers 6

JPA Key Concepts Entities Persistence Units Persistence Contexts 7

JPA Entities Plain old Java objects: No required interfaces Created using new Foo() Support inheritance, polymorphism Have persistent identity May have both persistent and non-persistent state Usable outside the container: Serializable Can be used as a detached object 8

JPA Entities Queryable via Java Persistence query language Similar to SQL, but extended for O/R mapping Dynamically constructed query strings Named Queries embedded in entity classes Managed at runtime through the Entity Manager APIs 9

JPA Entity Classes @Entity public class Customer { } @Id private long id; private String name; // Non-persistent data @OneToMany List<Order> orders = new ArrayList(); public List<Order> getorders() { return orders; } public void addorder(order order) { getorders().add(order); } 10

JPA Persistence Unit Unit of persistence packaging and deployment Set of managed classes: Entities Related classes (primary keys, etc.) Defines scope for: Queries Entity relationships Object/relational mapping information: Java language annotations and/or XML files Configuration information for provider: META-INF/persistence.xml file 11

JPA Persistence Unit Sample persistence.xml (shale-mailreader-jpa): <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="mailreaderjpa" transaction-type="resource_local"> <provider>oracle.toplink.essentials.ejb.cmp3. EntityManagerFactoryProvider</provider> <non-jta-data-source>jdbc/mailreader </non-jta-data-source> <properties> <property name="toplink.ddl-generation" value="create-tables"/> </properties> </persistence-unit> </persistence> 12

JPA Persistence Context Runtime application execution context Set of managed bean instances, belonging to a single persistence unit: Entities that have been read from the database Entities that will be written to the database Including newly persistent entities Persistent entity identity == Java identity Persistence context lifetime is either: Single transaction scoped Extended spanning multiple transactions 13

JPA Persistence Contexts Managed by either container or application Container managed persistence contexts: Provide ease of use in Java EE environment Propogated across components if using JTA Obtained by injection or JNDI lookup May be single-transaction or extended Application managed persistence contexts: Use in either Java SE or Java EE environment Obtained from EntityManagerFactory Extended scope must be managed by app also Web tier supports both approaches 14

JPA EntityManager API Entity lifecycle operations: persist(), remove(), refresh(), merge() Finder operations: find() (by primary key), getreference() Factory for query objects: createquery(), createnamedquery(), createnativequery() Operations to manage persistence context: Flush(), clear(), close(), gettransaction(), jointransaction() 15

JPA Persisting an Entity @PersistenceContext private EntityManager em;... public Order addneworder(customer cust, Product prod) { } Order order = new Order(prod); cust.addorder(order); em.persist(order); return order; 16

JPA Removing an Entity @PersistenceContext private EntityManager em;... public void dropcustomer(long custid) { } Customer cust = em.find(customer.class, custid); em.remove(cust); 17

JPA EntityManagerFactory Factory for EntityManager instances CreateEntityManager() Allows pluggable replacement of JPA implementation in Java SE environment Container will have picked a particular implementation Injectable via @PersistenceUnit annotation 18

JPA Other Issues Resource injection: Applies to container-managed persistence contexts only Performs annotation based dependency injection... @PersistenceContext, @PersistenceUnit,... On container-created objects only: Servlet, Filter, Listener, JSF Managed Bean Alternatives: JNDI lookup, Spring, programmatic access Thread safety: EntityManager instance is not thread safe EntityManagerFactory is thread safe 19

Overall Design Patterns Three basic patterns for integrating Shale/JSF and JPA: All in one backing bean POJO based business logic Session EJB based business logic Plus a more radical alternative (JBoss Seam): Backing bean is a stateful session bean 20

All In One Backing Bean Combines model access and business logic directly into backing bean class Suitable when required logic is extremely simple Characteristics: EntityManager instance injected by container Therefore, must be request scoped for thread safety Event handler performs required data access Example: user login authentication 21

All In One Backing Bean public class LogonBean { // EntityManager is injected by the container @PersistenceContext EntityManager em; // Properties for username/password (bound to components) private String username; public String getusername() { return username; } public void setusername(string username) { this.username = username; } private String password; public String getpassword() { return password; } public void setpassword(string password) { this.password = password; }... 22

All In One Backing Bean // Action method bound to submit button public String authenticate() { try { Query query = em.createquery ( select User u from Users where u.username = + :username and u.password = :password ); em.createnamedquery( User.findByUsernamePassword ); query.setparameter( username, username); query.setparameter( password, password); User user = (User) query.getsingleresult();... store user in session to denote logged in status... return success ; } catch (NoResultException e) {... store error message to be redisplayed... return null; // Redisplay current page } } } 23

POJO Based Business Logic Often, business logic (and model tier access) is: Nontrivial Reusable Standard design pattern is to encapsulate in a separate Java class: In Java-based apps, often a POJO In webapps, typically stored in application scope So, must deal with multithreaded access Let's look at such a business logic bean first... 24

POJO Based Business Logic public class Logic { // EntityManagerFactory is injected by the container @PersistenceUnit EntityManagerFactory emf; // Typical business logic method public Subscription createsubscription(subscription subs) { EntityManager em = emf.createentitymanager(); EntityTransaction et = null; try { et = em.gettransaction(); et.begin(); User user = em.find(user.class, subs.getuser().getid(); user.addsubscription(subs); em.persist(subs); et.commit; } catch (Exception e) {... } finally { if ((et!= null) && et.isactive()) { et.rollback(); } em.close(); } } } 25

POJO Based Business Logic JSF can inject an instance of the business logic bean into backing beans for you as well: Define a logic property of type Logic Configure the business logic managed bean: <managed-bean> <managed-bean-name>backing</managed-bean-name> <managed-bean-class>...<managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>logic</property-name> <value>#{logic}</value> </managed-property> </managed-bean> Thus, backing beans have direct access to logic 26

Session Bean Business Logic In our business logic bean, we had to deal with transactions explicitly Consider using a stateless session bean (EJB) Transactions managed by the container Also get other benefits: No more thread safety concerns Scalability (across server instances) Participate in cross-resource transactions What adjustments would we have to make to our POJO example? 27

Session Bean Business Logic Define a business interface for our logic: public interface BusinessLogic {... } Make our logic class implement the interface... And add simple annotations: @Local @Stateless public class Logic implements BusinessLogic... And we have just created a stateless session bean which can be injected into our backing beans: @EJB private Logic logic; We now have direct access to business methods 28

The JBoss Seam Alternative We won't have time to delve into all the details... But Seam offers an interesting alternative: Combine backing bean and business logic bean into one Theory: the reusable business logic isn't always reusable Theory: the backing bean's logic is just glue While we are at it, we can use stateful session bean Maintain conversational state across HTTP requests... Using the EJB container instead of HTTP sessions 29

Other Design Notes Binding UI component values: Typical pattern: bind to properties of backing bean In Struts, this would have been the form bean Result: lots of copying values back and forth Instead, make a JPA entity class a property of your backing bean, and bind to it directly Passing state between requests: Typical pattern: pass primary keys around Alternative: JPA entity instances can be detached and sent along with the JSF component state 30

Other Design Notes Creating bookmarkable URLs: Shale's ViewController callbacks can help here Two different HTTP accesses are supported: GET: init() --> prerender() --> destroy() POST: init() --> preprocess() --> action method --> prerender() -- > destroy() Can also access request parameters for keys We will see a worked out demonstration shortly 31

Demo 32

Today's News Shale has a brand new logo image: And a powered by logo: Congrats to Walied Amer, logo contest winner 33

Questions and Answers 34