Book Image

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Book Image

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Overview of this book

EJB (Enterprise JavaBeans) 3.0 is a commonly used database persistence technology in Java EE applications. EJB 3.0 has simplified the development of EJBs with an annotations-based API that eliminates the use of remote/local interfaces, home/local home interfaces, and deployment descriptors. A number of other books are available on EJB 3.0, but none covers EJB 3.0 support in Oracle Fusion Middleware 11g, which is one of the leaders in the application server market.This is the first book that covers all aspects of EJB 3.0 database persistence development using Oracle Fusion Middleware technology. It covers all the best practices for database persistence ensuring that your applications are easily maintainable. Leaving theory behind, this book uses real-world examples to guide you in building your own EJB 3.0 applications that are well integrated with commonly used Java EE frameworks.The book gets going by discussing the new features in the EJB 3.0 specification. As some readers may still be using EJB 2.0, the book explains how to convert your EJB 2.0 entity beans to EJB 3.0. It then goes on to discuss using EJB 3.0 database persistence with JDeveloper, WebLogic Server, and Enterprise Pack for Eclipse, the main Java EE components of Oracle Fusion Middleware 11g. The book also covers EJB 3.0 relationships and integrating EJB 3.0 relationships with JSF user interfaces. EJB 3.0 database persistence with some of the commonly used frameworks such as ADF Faces, AJAX, and Web Services is also discussed in the book. It uses the integrated WebLogic Server 11g in some of the chapters and the standalone WebLogic Server in other chapters. While JDeveloper is the primary Java IDE used in the book, one of the chapters is based on the Oracle Enterprise Pack for Eclipse.By the time you reach the end of this book, you will be well-versed with developing EJB 3.0 applications using the different Java EE components of Oracle Fusion Middleware 11g.
Table of Contents (15 chapters)
EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g
Credits
About the Author
About the Reviewers
Preface

Java Persistence API


The Java Persistence API (JPA) is the persistence component of EJB 3.0. "An EJB 3.0 entity is a lightweight persistent domain object." As discussed in the previous section, the entity class is a POJO annotated with the @Entity annotation. The relationship modeling annotations @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany, are used for object/relational mapping of entity associations. EJB 3.0 specifies the object/relational mapping defaults for entity associations.

The annotations for object/relational mapping are defined in the javax.persistence package. An entity instance is created with the new operator and persisted using the EntityManager API. An EntityManager is injected into an entity bean using the @PersistenceContext annotation:

@PersistenceContext
EntityManager em;

An entity instance is persisted using the persist() method:

CatalogBean catalogBean=new CatalogBean();
em.persist(catalogBean);

The EntityManager is also used to remove entity instances using the remove() method:

em.remove(catalogBean);

EntityManager is also used to find entities by their primary key with the find method:

CatalogBean catalogbean=(CatalogBean)(em.find("CatalogBean", catalogId));

The @NamedQuery annotation is used to specify a named query in the Java Persistence Query language, which is an extension of EJB-QL. The Java Persistence Query language further adds operations for bulk update and delete, JOIN operations, GROUP BY, HAVING, and subqueries, and also supports dynamic queries and named parameters. Queries may also be specified in native SQL.

@NamedQuery(
name="findAllBlogsByName",
query="SELECT b FROM Blog b WHERE b.name LIKE :blogName"
)

The EntityManager is used to query entities using a Query object created from a named query:

Query query = em.createNamedQuery("findAllBlogsByName");

The named query parameters are set using the setParameter() method:

query.setParameter("blogName", "Smythe");

A SELECT query is run using the getResultList() method. A SELECT query that returns a single result is run using the getSingleResult() method. An UPDATE or DELETE statement is run using the executeUpdate() method. For a query that returns a list, the maximum number of results may be set using the setMaxResults() method.

List blogs=query.getResultList();

A persistence unit defines a set of entities that are mapped to a single database and managed by an EntityManager. A persistence unit is defined in the persistence.xml deployment descriptor, which is packaged in the META-INF directory of an entity bean JAR file. The root element of the persistence.xml file is persistence, which has one or more persistence-unit sub-elements. The persistence-unit element consists of the name and transaction-type attributes and subelements description, provider, jta-data-source, non-jta-data-source, mapping-file, jar-file, class, exclude-unlisted-classes, and properties. Only the name attribute is required; the other attributes and subelements are optional. The jta-data-source and non-jta-data-source are used to specify the global JNDI name of the data source to be used by the persistence provider. For all the elements in the persistence.xml and a detailed discussion on Java Persistence API, refer to the EJB 3.0 specification (ejb-3_0-fr-spec-persistence.pdf).