Java EE 6 SDK 

Samples Main Page

The EJB 3.1 Embeddable API Sample Application

This is a very simple EJB Stateless Session Bean with a no-interface view and a POJO Entity that demonstrates EJB 3.1 Embeddable API usage.

Application Structure

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.

EJB Module

The EJB module consists of:

No-interface View
The Stateless Session bean has a no-interface with 2 business methods

public int verify()

public void insert(int num)
 

Stateless Session Bean Class
The bean implementation is as follows:

@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);
        }
    }
}


Deployment Descriptor
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.

Building, Deploying, and Running the Application

Perform the following steps to build, and run the application:
  1. Setup your build environment and configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee6/ejb/ejb-embedded.
  3. Change directory to app_dir.
  4. Make sure the JavaDB database is started.
  5. Set up JDBC resources. Build, and run the sample application using the all target.
  6. 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

  7. Use the target clean to undeploy the sample application and to remove the temporary directories like build and dist.

    app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Perform the following steps to build, deploy, and run the application using NetBeans IDE:

  1. Refer to the common build instructions for setting up NetBeans IDE and Java EE 6 SDK.
  2. In the NetBeans IDE, choose File → Open Project (Ctrl-Shift-O), navigate to the samples_install_dir/javaee6/ejb/ directory, select ejb-embedded, and click Open Project.
  3. In the Projects tab, right click ejb-embedded and select Test to observe the sample.

Troubleshooting

If you have problems when running the application, refer the troubleshooting document.

 

Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.