Oracle: Developer Day
Oracle: Developer Day
Sponsored by:
Track # 1: Session #2
Page 1
1
Agenda
Introduction to the EJB 3.0 Specification
What are Enterprise JavaBeans (EJB)? Goals Comparison with 2.1 specification
Enterprise JavaBeans
Middle-tier Java components encapsulating business logic Hosted in EJB containers Provide services for clients
Business logic Persistence Messaging
Page 2
2
Types of EJBs
EJB Type
Session Beans
Purpose
Performs a business task for a client Represents a business object that exists in a database Receives asynchronous Java Message Service (JMS) messages
Entity Beans
Message-Driven Beans
Page 3
3
Classes Classes
1000 900 800 700 600 500 400 300 200 100 0 Lines of Java Lines of XML Lines Lines
of XML
of Java
Page 4
4
Agenda
Introduction to the 3.0 Specification
Goals Comparison with 2.1 specification
EJB Persistence
Simple programming model
Proven POJO persistence O-R mapping annotations and Entity Manager API
Page 5
5
POJO Entities
Concrete classes (no longer abstract) No required interfaces Support new() getter/setter methods with annotations
Page 6
6
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
Single table per class hierarchy Table per class Joined subclass
Default type mappings defined by specification Custom type mappings for finer control and flexibility
Page 7
7
EntityManager
EntityManager serves as untyped home Provides lifecycle operations
persist() remove() merge() flush(), refresh(), etc.
Page 8
8
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 of other Java classes
Page 9
9
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
Page 10
10
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)
@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() { } }
Page 11
11
Dependency Injection
Resources
DataSource, JMS, etc
@Resource(name="jms/lodging/QueueConnectionFactory") private QueueConnectionFactory queueConnectionFactory;
Environments
EJB Context, environment variables
Other EJBs
Session Beans , entities (using EM API)
@Resource public EntityManager em;
Page 12
12
D E M O N S T R A T I O N
Page 13
13
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 on EJB 3.0 persistence EJB3 Resource Center: http://otn.oracle.com/ejb3
Page 14
14
Q & A
Page 15
QUESTIONS ANSWERS
15
EJB QL Enhancements
Bulk update and delete operations Projection list (SELECT clause) Group by, Having Sub-queries (correlated and not) Additional SQL functions
Page 16
16
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
Page 17
17
Interceptor Example
@Stateless @Interceptor(value="oracle.ejb30.ProfilingInterceptor") // identify external interceptors public class HelloWorldBean implements HelloWorld {
Modify parameters before they're passed to the bean Modify the@AroundInvoke value returned from the bean Catch and// swallow method exceptions mark this method as a bean interceptor Interrupt the call completely (handy for a home-grown security framework) public Object checkPermission(InvocationContext Provide method profiling
Agenda
Introduction to the 3.0 Specification
Goals Comparison with 2.1 specification
Page 18
18