Application Overview

The customere sample is a very simple demonstration of a container managed jpa persistence context accessed from a stateless session bean, in turn accessed from a jsp based web app or standalone java client. Since jsps do no support dependency injection through annotations the ejb is looked up in an auxiliary class using the "legacy" java:comp/env jndi context.

JPA details is the entity bean that represents the Customer table in the database. By using @Entity, @Table(name = "customer"), and @Id it tells OpenEJB that this is an entity bean, which is representative of the table "customer" and has "customerId" as the primary key. By using these annotations no other configuration is needed inside openejb-jar.xml includes 2 named queries, one is called AllCustomers; the other is called FindCustmer.
package com.service.customer.ejb;


import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Table(name = "CustomerInfo")
@NamedQueries({@NamedQuery(name = "AllCustomers", query = "SELECT c from CustomerInfo c"),
        @NamedQuery(name = "FindCustomer", query = "SELECT c from CustomerInfo c  WHERE c.customerId=:customerId")})
public class CustomerInfo implements Serializable {
    private String customerId;
    private String fullName;
    private String emailAddress;
    private String interests;

    public CustomerInfo() {


    public CustomerInfo(String customerId, String fullName, String emailAddress,
                        String interests) {
        this.customerId = customerId;
        this.fullName = fullName;
        this.emailAddress = emailAddress;
        this.interests = interests;

    public String getCustomerId() {
        return customerId;

    public String getFullName() {
        return fullName;

    public String getEmailAddress() {
        return emailAddress;

    public String getInterests() {
        return interests;

    public void setCustomerId(String customerId) {
        this.customerId = customerId;

    public void setFullName(String fullName) {
        this.fullName = fullName;

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;

    public void setInterests(String interests) {
        this.interests = interests;
} // end CustomerInfo

Query AllCustmers is used from the stateless session bean called ProcessCustomerSessionBean, which relies on injection of a PersistenceContext; thus we are using container managed persistence contexts, and work is executed in a jta CMT transaction. The ProcessCustomerSessionBean class implements all the methods defined in its local and remote interfaces.
package com.service.customer.ejb;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

public class ProcessCustomerSessionBean {
    protected EntityManager em;

    public ProcessCustomerSessionBean() {


    public List<CustomerInfo> findAllCustomers() {
        Query q = em.createNamedQuery("AllCustomers");
        List<CustomerInfo> customerList = q.getResultList();
        return customerList;

    public CustomerInfo findCustomer(String key) {
        Query q = em.createNamedQuery("FindCustomer");
        q.setParameter("customerId", key);
        CustomerInfo c = (CustomerInfo) q.getSingleResult();
        return c;

persistence.xml references certain Entity Beans and maps them to use certain database pools. In this case, the entity bean Customer is using the SampleTxDatasourc and SampleNoTxDatasource database pools.

<persistence xmlns=""
             xmlns:xsi="" version="1.0"
    <persistence-unit name="CustomerPU">
        <description>Entity Beans for CustomerInfo</description>
            <property name="openjpa.jdbc.SynchronizeMappings" value="false"/>

web.xml references the EJB that was created in By doing this we are allowing the JSP contents inside the WAR to use this EJB via JNDI.

<web-app xmlns=""




The app is visible at http://localhost:8080/customer/

  • No labels