Java EE 6 SDK |
The application is built as an EJB module. It is deployed during creation of the embeddable EJB container. It is undeployed when the embeddable EJB container is closed.
The EJB module consists of:
A POJO that represents a SimpleEntity
.
The persistence.xml
file defines a persistence unit. The
persistence
unit uses jdbc/__default
as the data source, and the java2db
feature of
glassfish to create tables.
An EJB SimpleEjb
that
uses JPA to persist entities in and fetch entities from
the database.
The Stateless Session bean has a no-interface with 2 business methods
public int verify()
public void insert(int num)
The bean implementation is as follows:Deployment Descriptor
@Stateless
public class SimpleEjb {
@PersistenceContext(unitName="embedded_test") EntityManager em;
@PermitAll
public int verify() {
String result = null;
Query q = em.createNamedQuery("SimpleEntity.findAll");
Collection entities = q.getResultList();
int s = entities.size();
for (Object o : entities) {
SimpleEntity se = (SimpleEntity)o;
System.out.println("Found: " + se.getName());
}
return s;
}
@PermitAll
public void insert(int num) {
for (int i = 1; i <= num; i++) {
System.out.println("Inserting # " + i);
SimpleEntity e = new SimpleEntity(i);
em.persist(e);
}
}
}
Only persistence deployment descriptor is required.JNDI Lookup
The global JNDI name used for lookup of the Stateless
Session Bean is : java:global/ejb-embedded/SimpleEjb.
The
EJB module name "ejb-embedded"
is defined by the unqualified jar name
when the example is built.
app_dir
is the sample application base
directory: samples_install_dir/javaee6/ejb/ejb-embedded
.
Change directory to app_dir.
all
target.
app_dir> ant
all
You can replace the ant all
command with the
following set of
commands:
app_dir> ant
default
compiles and packages the application
app_dir> ant
run
runs the test java client
app_dir> ant
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/javaee6/ejb/
directory, select ejb-embedded
, and click Open Project.ejb-embedded
and select Test to observe the sample.If you have problems when running the application, refer the troubleshooting document.
Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.