Oracle Developer Day Sponsored by: Sponsored by: Sponsored by: - - PDF document

oracle developer day
SMART_READER_LITE
LIVE PREVIEW

Oracle Developer Day Sponsored by: Sponsored by: Sponsored by: - - PDF document

Oracle Developer Day Sponsored by: Sponsored by: Sponsored by: Sponsored by: Track # 1: Session #2 Developing Enterprise JavaBeans using the EJB 3.0 Specification Page 1 1 Agenda Introduction to the EJB 3.0 Specification What


slide-1
SLIDE 1

1

Page 1

Sponsored by: Sponsored by: Sponsored by: Sponsored by:

Oracle Developer Day

Track # 1: Session #2

Developing Enterprise JavaBeans using the EJB 3.0 Specification

slide-2
SLIDE 2

2

Page 2

Agenda

  • Introduction to the EJB 3.0 Specification

– What are Enterprise JavaBeans (EJB)? – Goals – Comparison with 2.1 specification

  • Developing 3.0 Entity Beans (Entities)

– Persistence API

  • Developing 3.0 Session Beans

– Client view

Enterprise JavaBeans

  • Middle-tier Java components

encapsulating business logic

  • Hosted in EJB containers
  • Provide services for clients

– Business logic – Persistence – Messaging

slide-3
SLIDE 3

3

Page 3

Types of EJBs

EJB Type Purpose

Session Beans Performs a business task for a client Entity Beans Represents a business object that exists in a database Message-Driven Beans Receives asynchronous Java Message Service (JMS) messages

EJB 3.0 Goals

  • Simplify Development

– EJB = Plain Java Object (POJO) – Use metadata annotations – Reduce number of artifacts – Attract broad range of developers

  • Standardize persistence API

– O-R Mapping similar to Oracle TopLink, Hibernate – EntityManager API for CRUD Operations

slide-4
SLIDE 4

4

Page 4

EJB 3.0 Simplification

POJO and POJI

– Removes complexity of earlier versions using simple and familiar Java artifacts – EJB Class will be a POJO – EJB Interface will be a POJI (no EJB extensions) – No need for home interface – Annotations for type of EJB and interface

EJB 3.0 Simplification Analysis

100 200 300 400 500 600 700 800 900 1000 Lines of Java Lines of XML 2 4 6 8 10 12 14 16 18 Classes XML Descriptors

EJB 2.1 EJB 3.0

Classes XML Descriptors Lines

  • f XML

Lines

  • f Java

The Simplicity of EJB 3.0 http://java.sys-con.com/read/117755.htm

slide-5
SLIDE 5

5

Page 5

Agenda

  • Introduction to the 3.0 Specification

– Goals – Comparison with 2.1 specification

  • Developing 3.0 Entity Beans (Entities)

– Persistence API

  • Developing 3.0 Session Beans

– Client view

EJB Persistence

  • Simple programming model

– Proven POJO persistence – O-R mapping annotations and Entity Manager API

  • Improved modelling capabilities

– Inheritance and polymorphism

  • Entities usable outside the container

– Facilitates testability

slide-6
SLIDE 6

6

Page 6

POJO Entities

  • Concrete classes (no longer abstract)
  • No required interfaces
  • Support new()
  • getter/setter methods with annotations

– can contain logic (validation, etc.)

  • Collection interfaces used for relationships
  • Usable outside the EJB container

Example: EJB 3.0 Entity

@Entity public class Customer { private Long id; private String name; private HashSet orders = new HashSet(); @Id (generate=SEQUENCE, generator="SEQ_GEN") @SequenceGenerator(name="SEQ_GEN", @ sequenceName="CUST_SEQ",allocationSize=1) @Column(name = "ID", primaryKey = true) public Long getId() { return id; } protected void setId (Long id) { this.id = id; } ...

slide-7
SLIDE 7

7

Page 7

Example: EJB 3.0 Entity

... @OneToMany(cascade=ALL, mappedBy=“customer”) public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } // other business methods, etc. }

O-R Mapping

  • Use Java application metadata annotations or XML to

specify mapping

  • Ability to map one or more persistent object to a table

– Embeddable

  • Support for typical inheritance strategies

– Single table per class hierarchy – Table per class – Joined subclass

  • Default type mappings defined by specification
  • Custom type mappings for finer control and flexibility
slide-8
SLIDE 8

8

Page 8

EntityManager

  • EntityManager serves as untyped

“home”

  • Provides lifecycle operations

– persist() – remove() – merge() – flush(), refresh(), etc.

  • Factory for Query objects

EntityManager API example

@Resource public EntityManager em; .. public void addLoan(String provider, long term, String loan_type, double interest_rate) { Loans loan = new Loans(); loan.setProvider(provider); loan.setTerm(term); loan.setLoanType(loan_type); loan.setInterestRate(interest_rate); em.persist(loan); }

slide-9
SLIDE 9

9

Page 9

Query API

  • Utilize EJBQL, Expressions, SQL
  • Defined dynamically or stored within

an Entity

public List findWithName (String name) { return em.createQuery ( “SELECT c FROM Customer c” + “WHERE c.name LIKE :custName”) .setParameter(“custName”, name) .setMaxResults(10) .getResultList(); }

SQL

  • Allow direct SQL over actual

database schema

– Very useful for some applications – Database portability overrated for some applications

  • Allow SQL query results to be

mapped into entities and/or instances

  • f other Java classes
slide-10
SLIDE 10

10

Page 10

D E M O N S T R A T I O N

Developing Entities

Agenda

  • Introduction to the 3.0 Specification

– Goals – Comparison with 2.1 specification

  • Developing 3.0 Entity Beans (Entities)

– Persistence API

  • Developing 3.0 Session Beans

– Client view

slide-11
SLIDE 11

11

Page 11

EJB 3.0 Session Beans

  • Java class with @Stateless/Stateful annotation
  • Business interface is a POJI

– Interface with @Local/Remote/WebService annotation(s) – RemoteExceptions removed from programmer and client view

  • Home Interface not required
  • No EJB extensions required for bean or

interface(s)

EJB 3.0 Session

@Remote public interface Cart { public void addItem(String item); public void completeOrder(); public Collection getItems(); } @Stateful public class CartBean implements Cart { private ArrayList items; public void addItem(String item) { items.add(item); } public Collection getItems() { return items; } @Remove public void completeOrder() { … } }

slide-12
SLIDE 12

12

Page 12

Dependency Injection

  • Resources

– DataSource, JMS, etc

  • Environments

– EJB Context, environment variables

  • Other EJBs

– Session Beans , entities (using EM API)

@Resource(name="jms/lodging/QueueConnectionFactory") private QueueConnectionFactory queueConnectionFactory; @Resource public EntityManager em;

Enhanced Lifecycle Methods

  • No need to implement unnecessary call

back methods

  • Mark any arbitrary methods as callback

method using annotations or XML

@PostConstruct public void initialize() { items = new ArrayList(); }

slide-13
SLIDE 13

13

Page 13

EJB 3.0 Client View

@Stateful public class OrderBean { //Dependency injection to use another EJB @EJB CartEJB cart; public void addItems() { cart.addItem("Item1"); } }

D E M O N S T R A T I O N

Developing Session Beans

slide-14
SLIDE 14

14

Page 14

Oracle and EJB 3.0

  • Oracle is co-specification lead for EJB 3.0

– Oracle to contribute the reference implementation for EJB 3.0 based on TopLink

  • Oracle is a leader in J2EE and first major

application server vendor to support EJB 3.0

– Will easily facilitate migration to EJB 3.0

  • Oracle is leading an Eclipse tools project
  • n EJB 3.0 persistence
  • EJB3 Resource Center:

http://otn.oracle.com/ejb3 Join Over 4,500,000 Developers! Join Over 4,500,000 Developers! Join Over 4,500,000 Developers! Join Over 4,500,000 Developers! Free Software Downloads Free Software Downloads Free Software Downloads Free Software Downloads Free Technical Advice Free Technical Advice Free Technical Advice Free Technical Advice

  • tn
  • tn
  • tn
  • tn.oracle.com/ejb3

.oracle.com/ejb3 .oracle.com/ejb3 .oracle.com/ejb3

  • tn
  • tn
  • tn
  • tn.oracle.com/tech/java

.oracle.com/tech/java .oracle.com/tech/java .oracle.com/tech/java

  • tn
  • tn
  • tn
  • tn.oracle.com/

.oracle.com/ .oracle.com/ .oracle.com/bpel bpel bpel bpel

slide-15
SLIDE 15

15

Page 15

Learn Oracle From Oracle

  • Instructor led training
  • Self-Study
  • Online learning
  • Oracle Certification
  • Oracle iLearning
  • Oracle Tutor
  • racle.com/education

A Q &

Q U E S T I O N S Q U E S T I O N S A N S W E R S A N S W E R S

slide-16
SLIDE 16

16

Page 16

Sponsored by: Sponsored by: Sponsored by: Sponsored by:

Oracle Developer Day

EJB QL Enhancements

  • Bulk update and delete operations
  • Projection list (SELECT clause)
  • Group by, Having
  • Sub-queries (correlated and not)
  • Additional SQL functions

– UPPER, LOWER, TRIM, CURRENT_DATE, ...

  • Dynamic queries
slide-17
SLIDE 17

17

Page 17

Named Queries

@NamedQuery( name= “findCustomersByName”, queryString= “SELECT c FROM Customer c” + “WHERE c.name LIKE :custName” ) @Resource public EntityManager em; ... List customers = em.createNamedQuery(“findCustomersByName”) .setParameter(“custName”, “Smith”) .getResultList();

Interceptors

  • Provides fine grained control over the

method invocation flow

– may be either a method in the same bean class or an external class – Used with SLSB, SFSB, MDB

  • Usage

– Modify parameters before they're passed to the bean – Modify the value returned from the bean – Catch and swallow method exceptions – Interrupt the call completely (handy for a home-grown security framework) – Provide method profiling a

slide-18
SLIDE 18

18

Page 18

Interceptor Example

  • Modify parameters before they're passed to the bean
  • Modify the value returned from the bean
  • Catch and swallow method exceptions
  • Interrupt the call completely (handy for a home-grown security framework)
  • Provide method profiling

@Stateless @Interceptor(value="oracle.ejb30.ProfilingInterceptor") // identify external interceptors public class HelloWorldBean implements HelloWorld { @AroundInvoke // mark this method as a bean interceptor public Object checkPermission(InvocationContext ctx) throws Exception { System.out.println ("*** checkPermission interceptor invoked"); … }

Agenda

  • Introduction to the 3.0 Specification

– Goals – Comparison with 2.1 specification

  • Developing 3.0 Entity Beans (Entities)

– Persistence API

  • Developing 3.0 Session Beans

– Client view