View
226
Download
0
Category
Preview:
Citation preview
7/28/2019 Enterprise Java Beans 301641 (1)
1/30
7/28/2019 Enterprise Java Beans 301641 (1)
2/30
Enterprise Java Bean 3.0
Filippo Diotalevihttp://www.diotalevi.com
filippo.diotalevi@jugmilano.it
Java User Group Milanohttp://www.jugmilano.it
February 16th, 2006
http://www.diotalevi.com/mailto:filippo.diotalevi@jugmilano.itmailto:filippo.diotalevi@jugmilano.ithttp://www.diotalevi.com/7/28/2019 Enterprise Java Beans 301641 (1)
3/30
JUG Milano EJB 3.0 3
Filippo Diotalevi
http://www.diotalevi.com/weblog
http://www.jugmilano.it
https://java-champions.dev.java.net/
7/28/2019 Enterprise Java Beans 301641 (1)
4/30JUG Milano EJB 3.0 4
Tag-Cloud
StatefulSessionBean
JavaEE5
EnterpriseJavaBeansStatelessSessionBean
EntityBean
JavaPersistenceAPIGlassfishAnnotations
ResourceInjection
POJOMessageDrivenBean
7/28/2019 Enterprise Java Beans 301641 (1)
5/30JUG Milano EJB 3.0 5
JavaEE5
EOD Easy of Development Increase productivity
Full backward compatibility
Community involvement
Goals
POJOAnnotations
ResourceInjectionTools:
7/28/2019 Enterprise Java Beans 301641 (1)
6/30JUG 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)
7/28/2019 Enterprise Java Beans 301641 (1)
7/30JUG 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
7/28/2019 Enterprise Java Beans 301641 (1)
8/30JUG Milano EJB 3.0 8
EJB 2.x Problems
Noisy
Metadata XML Hell Difficult test
Not truly OO
Pattern proliferation (DTO, ServiceLocator, ...)
7/28/2019 Enterprise Java Beans 301641 (1)
9/30JUG Milano EJB 3.0 9
EJB 3: Solutions
POJO Model
Annotations Configuration by exception
Java Persistence API
7/28/2019 Enterprise Java Beans 301641 (1)
10/30JUG 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)
7/28/2019 Enterprise Java Beans 301641 (1)
11/30JUG Milano EJB 3.0 11
public interface Calculator
{public int sum(int a, int b);
}
@Statelesspublic class CalculatorImpl implements Calculator
{
public int sum(int a, int b)
{
return a + b;
}
}
@Stateless Session Bean
@Remote
@Local
..or default (@Local)
7/28/2019 Enterprise Java Beans 301641 (1)
12/30JUG 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
7/28/2019 Enterprise Java Beans 301641 (1)
13/30
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)
7/28/2019 Enterprise Java Beans 301641 (1)
14/30
JUG Milano EJB 3.0 14
@Local
public 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
7/28/2019 Enterprise Java Beans 301641 (1)
15/30
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
7/28/2019 Enterprise Java Beans 301641 (1)
16/30
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 servicesend-point
i
7/28/2019 Enterprise Java Beans 301641 (1)
17/30
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
E tit B
7/28/2019 Enterprise Java Beans 301641 (1)
18/30
JUG Milano EJB 3.0 18
@Entity Beans
No more Home interfaces!
No Business Interfaces!
A POJO ImplementationAnnotated with @Entity
A simple deployment descriptor
E tit B
7/28/2019 Enterprise Java Beans 301641 (1)
19/30
JUG Milano EJB 3.0 19
@Entity Beans@Entity
public 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 Key
Id auto generation
(if not transient) Properties are automatically persisted
E tit M
7/28/2019 Enterprise Java Beans 301641 (1)
20/30
JUG Milano EJB 3.0 20
EntityManager
A detyped home interface
create(), merge() and
remove() instances
Factory for Query
E tit M
7/28/2019 Enterprise Java Beans 301641 (1)
21/30
JUG Milano EJB 3.0 21
EntityManager@Stateless
public 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!
@P i t C t t ?
7/28/2019 Enterprise Java Beans 301641 (1)
22/30
JUG Milano EJB 3.0 22
@PersistenceContext ?
persistence contextas "a set of entityinstances in which - for any persistent entityidentity -there is a unique entity instance."
Apersistence unitisa set of classes thatare mapped to asingle data store
persistence unit #2
Q er
7/28/2019 Enterprise Java Beans 301641 (1)
23/30
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()
Deployment Descriptor
7/28/2019 Enterprise Java Beans 301641 (1)
24/30
JUG Milano EJB 3.0 24
Deployment Descriptor
Need to define the Persistence Unit inpersistence.xml
jdbc/__default
Glassfish configuration
Entity Beans: advanced features
7/28/2019 Enterprise Java Beans 301641 (1)
25/30
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)
EJB Query Language
7/28/2019 Enterprise Java Beans 301641 (1)
26/30
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 c
SET c.status = 'outstanding'
WHERE c.balance < 10000
@MessageDriven Bean
7/28/2019 Enterprise Java Beans 301641 (1)
27/30
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)
Open Source EJB3 Implementations
7/28/2019 Enterprise Java Beans 301641 (1)
28/30
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)
@Questions?
7/28/2019 Enterprise Java Beans 301641 (1)
29/30
JUG Milano EJB 3.0 29
@Questions?
Thank you!
References
7/28/2019 Enterprise Java Beans 301641 (1)
30/30
JUG Milano EJB 3.0 30
References JSR 220: Enterprise JavaBeansTM 3.0
http://www.jcp.org/en/jsr/detail?id=220 Glassfish website and documentation
https://glassfish.dev.java.net
JBoss website and documentation
http://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
http://localhost/var/www/apps/conversion/tmp/scratch_14/https://glassfish.dev.java.net/http://www.jboss.org/http://hibernate.org/gavin/ebejb3.pdfhttp://www.javasig.com/Archive/lectures/JavaSIG-EJB30.pdfhttp://www.javasig.com/Archive/lectures/JavaSIG-EJB30.pdfhttp://hibernate.org/gavin/ebejb3.pdfhttp://www.jboss.org/https://glassfish.dev.java.net/http://localhost/var/www/apps/conversion/tmp/scratch_14/Recommended