Building an EJB 3.0 Persistence Model with Oracle JDeveloper

September 5, 2010

EJB 3.0

«»

Creating EJB 3.0 a session bean

One of the best practices in, developing an entity bean is to wrap it in a session bean
for a client. The entity bean is not directly accessed by a client. To create a session bean
select the EJB3Model project and select File | New. In the New Gallery window,
select Categories | Business Tier | EJB and Items | Session EJB. Click on OK.



Specify the EJB Name as CatalogTestSessionEJB. Select Session Type as Stateless
and Transaction Type as Container. We have selected the stateless session bean
because stateless session beans are less resource-intensive due to the lack of the
overhead to keep the state of a unique client-bean session. Select the default mapped
name (EJB3-SessionEJB).


The Generate Session Façade Methods checkbox is selected by default. The Entity
Implementation
is JPA Entities by default. The persistence unit is em. Click on Next.


Select the default JPA Entity Methods to create and click on Next.



Specify the Bean Class (model.CatalogTestSessionEJBBean) and click on Next.



Select the EJB business interface to implement. Select the Implement a Remote
Interface
checkbox, specify the Remote interface (model.CatalogTestSessionEJB).
Click on Next. The remote interface may be used in a distributed environment, but if
using the client and the EJB 3.0 model in the same JVM, the local client view may
be used.



The Summary page lists the session bean and the corresponding bean and interface
classes that will be generated. In the Summary window, click on Finish.



The session bean class


A session bean class CatalogTestSessionEJBBean gets added to the entity
bean model project. The remote business interface for the session bean,
CatalogTestSessionEJB, also gets created.



The CatalogTestSessionEJBBean cl ass is annotated with the annotation
@Stateless. The mappedName attribute specifies the global JNDifor the session
bean. We shall use the mapped name in the test client to lookup the session bean and
invoke method/s on it. The @Remote annotation indicates that the session bean is a
remote interface.



@Stateless(name = “CatalogTestSessionEJB”, mappedName = “EJB3-
SessionEJB”)
@Remote
public class CatalogTestSessionEJBBean implements
CatalogTestSessionEJB { }


In the session bean an EntityManager is injected using the @PersistenceContext
annotation . The unitName is specified, but not required, as the EntityManager
variable name is the same as the persistence unit name.



@PersistenceContext(unitName = “em”)
private EntityManager em;


Add a method test() to the session bean and the remote interface. In the test()
method, create a Catalog entity instance with the new operator:



Catalog catalog =new Catalog(“Kimberly Floss”, “Nov-Dec 2004″, new
Integer(1),”Oracle Magazine”, “Oracle Publishing”,”Database Resource
Manager”);


Invoke the persistEntity(Object) method to persist the entity bean instance:



persistEntity(catalog);


The persistEntity method invokes the persist method of the EntityManager to
persist the entity bean:



em.persist(entity);


Similarly, persist two more entity bean instances. Next, create an instance of the
Query object using the createQuery method to run a Java Persistence Query
Language statement. Bind an author name to the named parameter :name using
the setParameter method, and run the Java persistence query statement using the
getResultList method , which returns a List:



List catalogEntry =
em.createQuery(“SELECT c from Catalog c where
c.author=:name”).setParameter(“name”,”Jonas Jacobi”).getResultList();


Iterate over the List, which is actually just one catalog entry, to output field values
for the journal, publisher, edition, title, and author fields:



for (Iterator iter = catalogEntry.iterator(); iter.hasNext(); )
{Catalog element = (Catalog)iter.next();
retValue =retValue + “<br/>” + element.getJournal() +
“<br/>” + element.getPublisher() +
“<br/>” + element.getEdition() + “<br/>” +element.getTitle() + “<br/>”
+ element.getAuthor() +
“<br/>”;
}


Similarly, run a query to list all titles. Remove an entity instance using the remove
method of the EntityManager. Subsequently, run a query to list all the remaining
entity instances.



em.remove(catalog2);


The test method returns a String, which consists of a catalog entry, a list of all the
titles, and all the entity instances after removing an entity instance. The session bean
class is listed next:



package model;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless(name = “CatalogTestSessionEJB”,
mappedName = “EJB3-SessionEJB”)
@Remote
public class CatalogTestSessionEJBBean implements
CatalogTestSessionEJB {
@PersistenceContext(unitName = “em”)
private EntityManager em;
public CatalogTestSessionEJBBean() {
}
public String test() {
Catalog catalog =
new Catalog(“Kimberly Floss”, “Nov-Dec 2004″, new Integer(1),
“Oracle Magazine”, “Oracle Publishing”,
“Database Resource Manager”);
persistEntity(catalog);
Catalog catalog2 =
new Catalog(“Jonas Jacobi”, “Nov-Dec 2004″, new Integer(2),
“Oracle Magazine”, “Oracle Publishing”,
“From ADF UIX to JSF”);
persistEntity(catalog2);
Catalog catalog3 =
new Catalog(“Steve Muench”, “March-April 2005″, new Integer(3),
“Oracle Magazine”, “Oracle Publishing”,
“Starting with Oracle ADF”);
persistEntity(catalog3);
String retValue = “<b>A catalog entry: </b>”;
List catalogEntry =
em.createQuery(“SELECT c from Catalog c where c.author=:name”).
setParameter(“name”, “Jonas Jacobi”).getResultList();
for (Iterator iter = catalogEntry.iterator(); iter.hasNext(); ) {
Catalog element = (Catalog)iter.next();
retValue = retValue + “<br/>” + element.getJournal() + “<br/>” +
element.getPublisher() + “<br/>” +
element.getEdition() + “<br/>” +
element.getTitle() + “<br/>” + element.getAuthor() +
“<br/>”;
}
retValue = retValue + “<b>All Titles: </b>”;
List allTitles =
em.createQuery(“SELECT c from Catalog c”).getResultList();
for (Iterator iter = allTitles.iterator(); iter.hasNext(); ) {
Catalog element = (Catalog)iter.next();
retValue = retValue + “<br/>” + element.getTitle() + “<br/>”;
}
em.remove(catalog2);
retValue = retValue + “<b>All Entries after removing an entry:
</b>”;
List allCatalogEntries =
em.createQuery(“SELECT c from Catalog c”).getResultList();
for (Iterator iter = allCatalogEntries.iterator(); iter.hasNext(); )
{
Catalog element = (Catalog)iter.next();
retValue = retValue + “<br/>” + element + “<br/>”;
}
return retValue;
}
public Object mergeEntity(Object entity) {
return em.merge(entity);
}
public Object persistEntity(Object entity) {
em.persist(entity);
return entity;
}
/** <code>select o from Catalog o</code> */
public List<Catalog> queryCatalogFindAll() {
return em.createNamedQuery(“Catalog.findAll”).getResultList();
}
/** <code>select o from Catalog o</code> */
public List<Catalog> queryCatalogFindAllByRange(int firstResult,
int maxResults) {
Query query = em.createNamedQuery(“Catalog.findAll”);
if (firstResult > 0) {
query = query.setFirstResult(firstResult);
}
if (maxResults > 0) {
query = query.setMaxResults(maxResults);
}
return query.getResultList();
}
public void removeCatalog(Catalog catalog) {
catalog = em.find(Catalog.class, catalog.getId());
em.remove(catalog);
}
}


The remote business interface is listed next:



package model;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface CatalogTestSessionEJB {
List<Catalog> queryCatalogFindAll();
String test();
List<Catalog> queryCatalogFindAllByRange(int firstResult,
int maxResults);
void removeCatalog(Catalog catalog);
}

EJB 3.0 Articles

email

«»

Comments

comments