You are on page 1of 4

Enterprise JavaBeans 3.

0 Outline
to be released in 2006

● Rational for Change


● Concepts
– Annotations
Jacek Ratzinger – Dependency Injection
● EJB types and their specifics
Distributed Systems Group ● EJB-QL
Vienna University of Technology – standardize Hibernate?
● Client Programming Model
● Pros and Cons

Rational for Change Drive for EJB 3.0

● EJB 2.x has become overly complex ● decrease number of artefacts


● to many artifacts for developers – home interfaces and deployment descriptors
● to many interfaces in EJB 2.x (home, ejb) ● program annotations introduced in Java 5
● unnecessary exceptions, callback methods ● light-weight domain modelling
● no standard way to define a primary key – including inheritance and polymorphism
● configuration by exception
● EJB 3.0 makes another attempt promise ● dependency injection
reducing EJB's complexity for developers ● reduction of checked exceptions
● simpler domain models with Plain Old Java ● testing outside the container
Objects (POJOs)

Java 5.0 Annotations Dependency Injection


● annotations do not directly affect program ● beans may gain references to resources by
semantics having the container supply it with those
● can be inspected through source parsing or ● bean instance variables or setter methods
by using the additional reflection APIs are annotated as targets
● define custom annotations ● standard way = annotations
– annotate fields, methods, classes, etc. ● alternatively, lookup method of
● used to define javax.ejb.EJBContext or the JNDI APIs
– bean's business interface ● container responsibility: before use
– O/R mapping information (specific persistence)
– resource references
– deployment information

1
Dependency Injection Specifics of EJB 3.0

● annotation of instance variables ● Exceptions


@Resource(name="myDB") //type inferred – interface may declare arbitrary exceptions
public DataSource customerDB; – should not throw java.rmi.RemoteException
– EJBException thrown by the container
● setter injection
public DataSource customerDB; ● Callbacks and Callback Listener Classes
– no callbacks (e.g. ejbCreate(), ejbPassivate) for
@Resource(name=” myDB”) life cycle events required
public void setDataSource(DataSource myDB – method annotations for callbacks
this. customerDB = myDB; – callback listener class may be used instead of
}
callback methods

Entity Beans Entity Beans

● POJOs with annotation @Entity ● unidirectional and bidirectional relationships


● all fields persistent not marked with attributes of annotations
@Transient (configuration by exception) – one-to-one
● persistent once it is associated with an – one-to-many
EntityManager (em.persist(entity)) – many-to-one
– many-to-many
● owning side responsible for propagating
● approach: refer with annotations to concrete
relationship changes to database
tables/columns instead of the abstract
persistence schema

Sample Entity Bean EJB QL


@Entity public class Employee { ● EntitiyManager API similar to Hibernate and
Toplink
@Id(generate=GeneratorType.TABLE)
– persist(entity), find(Entity.class, id)
public Long getId() { return id;}
– createQuery(ejbqlString),
public Address getAddress() {
createNativeQuery(sqlString)
return address;}
● EJB QL Examples
@OneToMany(mappedBy=“leader”) – FROM Order order
public List<Employee> getTeamMembers() { – SELECT DISTINCT order.address.state FROM Order
order WHERE order.customer.name LIKE ?
return this.colleagues;}
– SELECT order FROM Order order INNER JOIN FETCH
order.customer
@Transient private void getFullName() {
return getFirstName() + getLastName()}

2
Enhancements to EJB QL Stateless Session Beans
● explicit inner and outer join ● plain Java object with a class-level
● fetch join for eager loading (default=lazy) annotation of @Stateless
● bulk update and delete ● can implement the javax.ejb.SessionBean
● subqueries interface, but is not required to
● group-by ● may implement a business interface
● support for native SQL queries – If no business interface implemented, generated
● dynamic query capability using all the public methods
● @BusinessMethod by default local
– @Remote indicates a remote interface

Sample Stateless Session Bean Web Services Session Beans


import javax.ejb.*; ● JSR-181
@Stateless ● a session bean that serves as a web service
@Remote endpoint is annotated with @WebService
public class HelloWorldBean {
● @WebMethod used to identify methods that
public String sayHello() {
return "Hello World"; are exposed as web service
} ● other annotations
}
– @WebParam, @WebResult, @OneWay,
@SOAPBinding, @SOAPMessageHandler,
@HandlerChain, @InitParam

Stateful Session Beans Sample Stateful Session Bean


● similar to Stateless @Stateful
public class AccountManagementBean {
● lifecycle event callbacks @EJB (name="mySessionBean") //dependency injection
– @PostConstruct: after dependency injection private MySessionI myBean;
– @PreDestroy: after method with @Remove private Socket socket;
– @PostActivate: equal to ejbActivate()
@PostConstruct
– @PrePassivate: equal to ejbPassivate() @PostActivate
● a callback listener class (@CallbackListener) public void initRemoteConnectionToAccountSystem() {
may be used instead of callback methods try {
this.socket = new Socket, PORT);}
catch (Exception ex) {throw new EJBException(ex);}
}
...

3
Message-driven Beans Bean Interfaces

● to a client, a message-driven bean is a ● session beans and message-driven beans


message consumer require a business interface
● message-listener business interface ● interfaces automatically generated from
determined by the messaging type in use implementation, if no interface implemented
● javax.jms.MessageListener for JMS ● interface name derived from class name
● if several interfaces implemented, annotation
required to define „the“ business interface
● interfaces assumed local unless annotated

Client Programming Model Pros and Cons

● EJB 3.0 remote or local clients access beans ● less artefacts for developers
through their business interface ● enterprise services for Java core (e.g.JBoss)
– EJB 3.0 compliant desktop application?
● business interfaces are ordinary Java
interfaces
● contrary to annotations provide deployment
● references to business interfaces through descriptors a holistic view to EJB modules
– injection mechanism ● vendor specific annotations defeats the goal
– javax.ejb.EJBContext.lookup()
of portability
– JNDI API
● don’t forget about configuration by exception
● entity beans: owning side defined by
dependent side=> less complexity?

You might also like