Java EE 7 SDK

Samples Main Page

# The Embeddable API EJB Sample Application (EJB Module)

This sample application demonstrates how to use the embeddable EJB container.

## Description

The EJB 3.2 Embeddable API Sample Application demonstrates how to use the embeddable EJB container defined in the Enterprise JavaBeans 3.2 specification. You can launch the embeddable EJB container from your code to run tests on EJBs outside of an application server environment.

The sample application consists of two parts: an EJB module (this project) and the test client. The rest of this document describes the EJB module.

The EJB module contains a stateless session bean with a no-interface view and a Java entity object. The client code creates the embeddable EJB container and deploys the EJB module, which consists of the following files:

- `SimpleEntity.java` is a Java entity object. The `persistence.xml` file defines a persistence unit which uses `jdbc/__default` as its data source and the `java2db` feature of GlassFish to create tables.
- `SimpleEjb.java` is an EJB that uses the Java Persistence API to store and retrieve entities from the data source.

The stateless session bean has a no-interface view with the following two business methods:

- `public int verify()`
- `public void insert(int num)`

The implementation of the stateless session bean is the following:

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

The sample application only requires the persistence deployment descriptor. The global JNDI name of the stateless session bean is `java:global/ejb-embedded/SimpleEjb`, where the EJB module name `ejb-embedded` corresponds to the unqualified name of the `.jar` file when building the application.

## Key Features

The EJB in the sample application demonstrates the following key features:

- `@PersistenceContext`
- `@Stateless`

## Building, Deploying, and Running the Application

Perform the following steps to build, deploy, 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/javaee7/ejb/ejb-embedded/ejb-embedded-server`.
3. ````Change directory to `app_dir.`
4. Build and install the sample application into your local repository

   `mvn clean install`

   This satisfies the dependency of the test client on this artifact.
5. Go to: `samples_install_dir/javaee7/ejb/ejb-embedded/ejb-embedded-client`.
6. Follow the instructions described in section "Building, Deploying, and Running the Application" in the project's document to build, deploy and run the client.

## Troubleshooting

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

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