270 likes | 370 Vues
Problems with EJB 2.1. Deployment Descriptors. Complex Multiple Partly vendor dependent Lack adequate defaults Better graphical tools needed. Verbosity. Need two interfaces to specify one EJB. Remote and local interface Remote and local home interface
E N D
Deployment Descriptors • Complex • Multiple • Partly vendor dependent • Lack adequate defaults • Better graphical tools needed
Verbosity • Need two interfaces to specify one EJB. • Remote and local interface • Remote and local home interface • Must implement special EJB classes. • Numerous callback methods required. • Even normally-empty callbacks like ejbPostCreate must be specified
EJB Query Language • EJB QL is currently weak. • Strange “object-oriented” syntax • Does not implement full SQL language • Lacks functions and operations • Programmers are forced to resort to JDBC and SQL. • EJB 2.1 lacks vendor-independent object-relational mapping tools.
Testing • Entity beans are abstract classes. • Consequently, they are completely untestable outside the context of the container.
What’s Coming in EJB 3.0 “EJB 3.0 Public Review Draft” is on your CD (in three parts). ”Proposed Final Review Draft” due to be approved in 2006.
Organization of the Specification Documents • “EJB 3.0 Simplified API” • “EJB 3.0 Core Contracts and Requirements” • “Java Persistence API”
EJB 3.0 Overview • Purpose is to reduce complexity of the EJB architecture from the enterprise application developer’s point of view. • Replace deployment descriptors with annotations in the source code (like javadoc comments). • Specify defaults representing the expected behaviors (“configuration by exceptions”). • Backward compatibility with EJB 2.1 (can mix and match). continued
EJB 3.0 Overview • Use of annotations to simplify JNDI lookup mechanisms and environmental dependencies. • Simplification of enterprise bean types. • Eliminate need for EJB component interfaces for session beans. Use plain old Java objects (POJOs) instead. • Eliminate need for home interfaces for session beans. continued
EJB 3.0 Overview • Simplify entity bean persistence. • Object-relational persistence API will work with Hibernate, TopLink and JDO. • Eliminate all required interfaces for entities written to the new persistence API. • Enhancement of EJB QL. • Eliminate the requirement for the implementation of callback interfaces. • Improve ability for testing outside the container continued
Acknowledgement • The following slides are based on code that was kindly provided by Bill Burke, Chief Architect at JBoss. • It will appear in his new O'Reilly book "Enterprise JavaBeans (Fifth Edition)".
Developing an Entity Bean package com.titan.domain; import javax.persistence.*; @Entity @Table(name="CABIN") public class Cabin implements java.io.Serializable{ private int id; private String name; private int deckLevel; private int shipId; private int bedCount; @Id @Column(name="CABIN_ID") public int getId() { return id; } public void setId(int pk) { id = pk; } @Column(name="CABIN_NAME") public String getName() { return name; } Cabin: The Bean Class continued
Developing an Entity Bean continued @Column(name="CABIN_NAME") public String getName() { return name; public void setName(String str) {name = str; } @Column(name="CABIN_DECK_LEVEL") public int getDeckLevel() { return deckLevel; } public void setDeckLevel(int level) { deckLevel = level; } @Column(name="CABIN_SHIP_ID") public int getShipId() { return shipId; } public void setShipId(int sid) { shipId = sid; } @Column(name="CABIN_BED_COUNT") public int getBedCount() { return bedCount; } public void setBedCount(int bed) { bedCount = bed; } }
Developing an Entity Bean • Annotations are objects, for example: • @javax.persistence.Entity specifes a persistent entity managed by an EntityManager service. • @javax.persistence.Table specifies the database table to map to. • @javax.persistence.Column defines a field in the database table • @Id specifies the primary key of a row. • Setters and getters are no longer abstract methods as in EJB 2.1. required
Developing an Entity Bean • Entity bean is a POJO. • Does not extend javax.ejb.EJBObject. • Bean class implements Serializable, therefore: • Entity classes can be used as parameters and return values. • Can be used as data transfer objects between client and server. • Can be persisted in a database.
persistence.xml <persistence> <persistence-unit> <name>titan</name> <jta-data-source>java:/DefaultDS</jta-data-source> </persistence-unit> </persistence>
Developing a Session Bean package com.titan.travelagent; import javax.ejb.Remote; import com.titan.domain.Cabin; @Remote public interface TravelAgentRemote { public void createCabin(Cabin cabin); public Cabin findCabin(int id); } TravelAgentRemote: The Remote Interface
Developing a Session Bean package com.titan.travelagent; import javax.ejb.Stateless; import javax.persistence.*; import com.titan.domain.Cabin; @Stateless public class TravelAgentBean implements TravelAgentRemote{ @PersistenceContext(unitName="titan") private EntityManager manager; public void createCabin(Cabin cabin) { manager.persist(cabin); } public Cabin findCabin(int pKey) { return manager.find(Cabin.class, pKey); } } TravelAgentBean: The Bean Class
Developing a Session Bean: Annotations • @javax.ejb.Remote: as opposed to Local. • @javax.ejb.Stateless: as opposed to Stateful. • @javax.persistence.PersistenceContext: gives access to the EntityManager.
CABIN Table in the Database create table CABIN ( ID int primary key NOT NULL, SHIP_ID int, BED_COUNT int, NAME char(30), DECK_LEVEL int )
Creating a Client Application package com.titan.clients; import com.titan.travelagent.TravelAgentRemote; import com.titan.domain.Cabin; import javax.naming.*; import java.util.Properties; import javax.rmi.PortableRemoteObject; public class Client_1 { public static void main(String [] args) { try { ontext jndiContext = new InitialContext(); // use the jndi properties file Object ref = jndiContext.lookup (TravelAgentRemote.class.getName()); TravelAgentRemote dao = ( TravelAgentRemote)PortableRemoteObject.narrow (ref,TravelAgentRemote.class); continued
Creating a Client Application continued Cabin cabin_1 = new Cabin(); cabin_1.setId(1); cabin_1.setName("Master Suite"); cabin_1.setDeckLevel(1); cabin_1.setShipId(1); cabin_1.setBedCount(3); dao.createCabin(cabin_1); Cabin cabin_2 = dao.findCabin(1); System.out.println(cabin_2.getName()); System.out.println(cabin_2.getDeckLevel()); System.out.println(cabin_2.getShipId()); System.out.println(cabin_2.getBedCount()); } catch (javax.naming.NamingException ne) {ne.printStackTrace();} } }
Coming May 1, 2006 “Enterprise JavaBeans 3.0” by Bill Burke and Richard Monson-Haefel, O’Reilly, ISBN 059600978X