Enterprise Java Beans 3.0
-
Upload
filippo-diotalevi -
Category
Technology
-
view
7.571 -
download
3
Transcript of Enterprise Java Beans 3.0
Enterprise Java Bean 3.0
Filippo Diotalevihttp://www.diotalevi.com
Java User Group Milanohttp://www.jugmilano.it
February 16th, 2006
JUG Milano – EJB 3.0 3
Filippo Diotalevi
http://www.diotalevi.com/weblog
http://www.jugmilano.it
https://java-champions.dev.java.net/
JUG Milano – EJB 3.0 4
Tag-Cloud
StatefulSessionBean
JavaEE5
EnterpriseJavaBeansStatelessSessionBean
EntityBean
JavaPersistenceAPIGlassfish
Annotations
ResourceInjection
POJOMessageDrivenBean
JUG Milano – EJB 3.0 5
JavaEE5
• EOD – Easy of Development
• Increase productivity
• Full backward compatibility
• Community involvement
Goals
POJOAnnotations
ResourceInjectionTools:
JUG Milano – EJB 3.0 6
JavaEE5: Key new Specs
• Java Persistence API (JSR-220)• EJB 3.0 (JSR-220)• JavaServer Faces (JSR-252)• JAX-WS (JSR-224)• JAXB (JSR-222)• Common Annotations (JSR-250)• StAX (JSR-173)
JUG Milano – EJB 3.0 7
EJB History
EJB 1.1 ( J2EE 1.2 ), 1999• Session beans (stateless & stateful), Entity Beans
• Remote interface
EJB 2.0 ( J2EE 1.3 ), 2001• Message-Driven Beans
• Entity 2.x and EJB QL
• Local and Remote interfaces
EJB 2.1 ( J2EE 1.4 ) 2003• EJB Timer Service
• Minor EJB QL enhancements
JUG Milano – EJB 3.0 8
EJB 2.x Problems
• “Noisy”• Metadata XML Hell• Difficult test• Not truly OO• Pattern proliferation (DTO, Service Locator, ...)
JUG Milano – EJB 3.0 9
EJB 3: Solutions
• POJO Model• Annotations• “Configuration by exception”• Java Persistence API
JUG Milano – EJB 3.0 10
@Stateless Session Bean
• No more Home interfaces!• A POJI Business Interface (optional!) Annotated with @Remote or @Local
• A POJO Implementation Annotated with @Stateless
• No more deployment descriptors! (optional)
JUG Milano – EJB 3.0 11
public interface Calculator { public int sum(int a, int b);}
@Stateless public class CalculatorImpl implements Calculator { public int sum(int a, int b) { return a + b; }}
@Stateless Session Bean
@Remote@Local
..or default (@Local)
JUG Milano – EJB 3.0 12
Transaction Management@Stateless public class CalculatorImpl implements Calculator { @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public int sum(int a, int b) { //.... }
TransactionAttributeType.MANDATORY
TransactionAttributeType.REQUIRED
TransactionAttributeType.REQUIRES_NEW
TransactionAttributeType.SUPPORTS
TransactionAttributeType.NOT_SUPPORTED
TransactionAttributeType.NEVER
JUG Milano – EJB 3.0 13
@Stateful Session Bean
• No more Home interfaces!• A POJI Business Interface (optional!) Annotated with @Remote or @Local
• A POJO Implementation Annotated with @Stateful
• No more deployment descriptors! (optional)
JUG Milano – EJB 3.0 14
@Localpublic interface CounterEjb {
public int count();public void reset();
}
@Statefulpublic class CounterEjbImplementation implements CounterEjb {
private int count = 0;public int count() {
return ++count;}public void reset() {
count = 0;}
}
@Stateful Session Bean
JUG Milano – EJB 3.0 15
@PostConstruct
@PostActivate
@PrePassivate
@PreDestroy
Other features
Callback methods
@Interceptors Declare interceptor classes (@Interceptors)
Develop an interceptor (POJO!)
Annotate interceptor's methods with @AroundInvoke
JUG Milano – EJB 3.0 16
@EJB Invocation & dependency injectionpublic class CountServlet extends HttpServlet{
@EJBpublic CounterEjb counter;
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
counter.count();}
} Dependency injection can only be used by managed classesServlet, listener classes, web services end-point, JAX-RPC handlers, EJB, interceptors, web services end-point
JUG Milano – EJB 3.0 17
Java Persistence API
• Separate specification document • Produced by EJB 3.0 EG (JSR-220)• Available in & outside the Java EE container
JUG Milano – EJB 3.0 18
@Entity Beans
• No more Home interfaces!• No Business Interfaces!• A POJO Implementation Annotated with @Entity
• A simple deployment descriptor
JUG Milano – EJB 3.0 19
@Entity Beans@Entitypublic class Item implements Serializable{
private Long id;private String name;
@Id@GeneratedValue(strategy = GenerationType.AUTO)public Long getId() { return id; }public void setId(Long id) { this.id = id; }
public String getName() { return name;}public void setName(String name) { this.name = name; }
}
Primary KeyId auto generation
(if not transient) Properties are automatically persisted
JUG Milano – EJB 3.0 20
EntityManager
• A detyped “home” interface• create(), merge() and remove() instances• Factory for Query
JUG Milano – EJB 3.0 21
EntityManager@Statelesspublic class PaginaDAOImpl implements PaginaDAO {
@PersistenceContext EntityManager entityManager;
public void salva(Pagina pagina) { entityManager.merge(pagina);
}public void elimina(Pagina pagina) {
entityManager.remove(pagina);}public Pagina leggi(String titolo) {
Pagina p = entityManager.find(Pagina.class, titolo);}
EntityManager injected!
JUG Milano – EJB 3.0 22
@PersistenceContext ?
persistence context as "a set of entity instances in which - for any persistent entity identity -there is a unique entity instance."
A persistence unit is a set of classes that are mapped to a single data store
persistence unit #2
JUG Milano – EJB 3.0 23
Query
Query query = entityManager.createQuery("SELECT p FROM Pagina p");return query.getResultList();
• Query parameters• Support for pagination• Native query
entityManager.createNativeQuery()
JUG Milano – EJB 3.0 24
Deployment Descriptor
Need to define the Persistence Unit in persistence.xml
<?xml version="1.0" encoding="UTF-8"?><persistence xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="defaultPersistenceUnit" transaction-type="JTA"> <jta-data-source>jdbc/__default</jta-data-source> <properties> <property name="toplink.platform.class.name" value="oracle.toplink.essentials.platform.database.DerbyPlatform"/> </properties> </persistence-unit></persistence> Glassfish configuration
JUG Milano – EJB 3.0 25
Entity Beans: advanced features
• Associations @OneToOne
@OneToMany
@ManyToOne
@ManyToMany
• Support for inheritance @Inheritance > 3 inheritance strategies: SINGLE TABLE
TABLE PER CLASS
JOINED TABLES
• Support for “cascadable” associations (@Cascade)• Support for optmistic locking (@Version)
JUG Milano – EJB 3.0 26
EJB Query Language
• Very similar to HQL (Hibernate Query Language)• Support for polymorphic queries• Support for native queries• Subselects
UPDATE Customer cSET c.status = 'outstanding'WHERE c.balance < 10000
JUG Milano – EJB 3.0 27
@MessageDriven Bean
• The Business Interface is the message listener interface (for JMS, javax.jms.MessageListener)
• A POJO Implementation Annotated with @MessageDriven
• No more deployment descriptors! (optional)
JUG Milano – EJB 3.0 28
Open Source EJB3 Implementations
http://jonas.objectweb.org/
http://www.jboss.org
https://glassfish.dev.java.net/
To be released: Open JPA (Solarmetric KODO)
JUG Milano – EJB 3.0 29
@Questions?
Thank you!
JUG Milano – EJB 3.0 30
References• JSR 220: Enterprise JavaBeansTM 3.0http://www.jcp.org/en/jsr/detail?id=220
• Glassfish website and documentationhttps://glassfish.dev.java.net
• JBoss website and documentationhttp://www.jboss.org
• Gavin King's “Entity Beans in EJB 3” http://hibernate.org/gavin/ebejb3.pdf
• Kenneth Saks “Enterprise Java Bean 3.0”http://www.javasig.com/Archive/lectures/JavaSIG-EJB30.pdf
• Filippo Diotalevi's “Java EE 5 – Step by Step”http://www.diotalevi.com/weblog/?page_id=125