Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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

Code Block
xml
xml
borderStylesolid
titlepersistence.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<persistence    xmlns="http://java.sun.com/xml/ns/persistence"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="CustomerPU">
        <description>Entity Beans for Customer</description>
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>com.service.customer.ejb.Customer</class>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings" value="false"/>
        </properties>
        <jta-data-source>CustomerServicePool</jta-data-source>
        <non-jta-data-source>CustomerServicePool</non-jta-data-source>
    </persistence-unit>
</persistence>

openejb-jar.xml is here because we are using OpenEJB. Nothing special is really defined in this file because we do not have any defined MDBs.

Code Block
xml
xml
borderStylesolid
titleopenejb-jar.xmlxml
<?xml version="1.0" encoding="UTF-8"?>

<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
             xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.1"
             xmlns:security="http://geronimo.apache.org/xml/ns/security-1.1"
             xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
   <dep:environment>
      <dep:moduleId>
         <dep:groupId>${pom.groupId}</dep:groupId>
         <dep:artifactId>${pom.artifactId}</dep:artifactId>
         <dep:version>${version}</dep:version>
         <dep:type>jar</dep:type>
      </dep:moduleId>

      <dep:dependencies>
      </dep:dependencies>
      <dep:hidden-classes/>
      <dep:non-overridable-classes/>
   </dep:environment>
</openejb-jar>

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

Code Block
xml
xml
borderStylesolid
titleweb.xmlxml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <ejb-local-ref>
    <ejb-ref-name>ejb/ProcessCustomerSessionBean</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>com.service.customer.ejb.ProcessCustomerSessionLocal</local>
  </ejb-local-ref>
</web-app>

geronimo-application.xml specifies the module's information and the context-root in which this web application resides. Additionally, it specifies the database pool plan that we want to create while deploying along with the connector that is needed to deploy this plan into Geronimo.

Code Block
xml
xml
borderStylesolid
titlegeronimo-application.xmlxml
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.1">
   <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
      <dep:moduleId>
         <dep:groupId>${pom.groupId}</dep:groupId>
         <dep:artifactId>${pom.artifactId}</dep:artifactId>
         <dep:version>${version}</dep:version>
         <dep:type>ear</dep:type>
      </dep:moduleId>

      <dep:dependencies/>
      <dep:hidden-classes/>
      <dep:non-overridable-classes/>
   </dep:environment>
   
   <module>
       <connector>tranql-connector-ra-1.3.rar</connector>
       <alt-dd>CustomerServicePool.xml</alt-dd>
   </module>
   
</application>

application.xml specifies a connector in which the EAR will use when trying to deploy the embedded database pool (CustomerServicePool.xml).

Code Block
xml
xml
borderStylesolid
titleapplication.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" version="5">
  <description>Geronimo Sample EAR for CustomerService</description>
  <display-name>Geronimo Sample EAR for CustomerService</display-name>
  <module>
    <ejb>CustomerService-ejb-2.0-SNAPSHOT.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>CustomerService-war-2.0-SNAPSHOT.war</web-uri>
      <context-root>/service</context-root>
    </web>
  </module>
  <module>
    <connector>tranql-connector-ra-1.3.rar</connector>
  </module>
</application>

CustomerServiceJavaBean.java uses JNDI to look up the ProcessCustomerSessionBean EJB.

Code Block
java
java
borderStylesolid
titleCustomerServiceJavaBean.javajava
package com.service.customer.web;

import com.service.customer.ejb.Customer;
import com.service.customer.ejb.ProcessCustomerSessionLocal;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.List;
import javax.naming.InitialContext;

public class CustomerServiceJavaBean
{
   private ProcessCustomerSessionLocal process = null;
   private ResourceBundle      bundle = null;

   public CustomerServiceJavaBean()
   {
      InitialContext initial = null;

      bundle = ResourceBundle.getBundle("customer", Locale.getDefault(), CustomerServiceJavaBean.class.getClassLoader());
      String jndiName = bundle.getString("jndi.process.ejb");

      try
      {
         initial = new InitialContext();
		 process = (ProcessCustomerSessionLocal) initial.lookup(jndiName.trim());
         System.out.println("Successful looking up: '" + jndiName.trim() + "'");
      } // end try

      catch (Exception e)
      {
         e.printStackTrace();
      } // end catch
   } // end CustomerServiceJavaBean


   public List<Customer> getAllCustomers()
   {
      List<Customer> customerList = null;

      try
      {
         customerList = process.findAllCustomers();
      } // end try
      catch (Exception e)
      {
         e.printStackTrace();
      } // end catch

      return customerList;
   } // end getAllCustomerss
} // end CustomerServiceJavaBean

ProcessCustomerSessionBean.java implements ProcessCustomerSessionLocal by grabbing an EntityManagerFactory by making use of the persistence.xml. It grabs it by using the @PersistenceUnit annotation. Since there is only one persistence unit defined in persistence.xml, we do not need to specify any additional parameters in the annotation.

Code Block
java
java
borderStylesolid
titleProcessCustomerSessionBean.javajava
package com.service.customer.ejb;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.persistence.PersistenceUnit;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

@Stateless
public class ProcessCustomerSessionBean implements ProcessCustomerSessionLocal {
    @PersistenceUnit
    protected EntityManagerFactory emf;

    public ProcessCustomerSessionBean() {
    
    }

    public List<Customer> findAllCustomers() {
        EntityManager em = emf.createEntityManager();
        String query = "SELECT * FROM customer";
        List<Customer> customerList =
            (List<Customer>)em.createNativeQuery(query, Customer.class).getResultList();
        em.close();
        return customerList;
    }

    public Customer findCustomer(String key) {
        EntityManager em = emf.createEntityManager();
        String query = "SELECT * FROM customer WHERE id='"+key+"'";
        List<Customer> customerList =
            (List<Customer>)em.createNativeQuery(query, Customer.class).getResultList();
        if(customerList.size() == 1) {
            return (Customer)customerList.get(0);
        } else {
            return null;
        }
    }
}

ProcessCustomerSessionLocal.java defines the business methods that is associated with this bean.

Code Block
java
java
borderStylesolid
titleProcessCustomerSessionLocal.javajava
package com.service.customer.ejb;

import com.service.customer.ejb.Customer;

public interface ProcessCustomerSessionLocal {
    public java.util.List<Customer> findAllCustomers();

    public Customer findCustomer(String key);
}

Customer.java 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 (no ejb-jar.xml is needed at all).

Code Block
java
java
borderStylesolid
titleCustomer.javajava
package com.service.customer.ejb;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "customer")
public class Customer implements Serializable {
    private String customerId;
    private String fullName;
    private String emailAddress;
    private String interests;

    public Customer() {

    }

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

    @Id
    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 Customer

...