Versions Compared

Key

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

...

And, as usual their is an empty constructor for the Entity Bean

Code Block
javajava
borderStylesolid
titlePhoneBook.java
java
package org.apache.geronimo.samples.myphonebookpak;

import java.io.Serializable;

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

@Entity
@Table(name = "phonebook")
public class PhoneBook implements Serializable {

	private String number;
	private String name;

	public PhoneBook() {

	}

	public PhoneBook(String name, String number) {
		this.name = name;
		this.number = number;
	}

	@Id
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
}

MyPhonebookLocal.java is the business interface that drives the above mentioned Entity Bean.

Code Block
javajava
borderStylesolid
titleMyPhonebookLocal.java
java
package org.apache.geronimo.samples.myphonebookpak;

import org.apache.geronimo.samples.myphonebookpak.PhoneBook;

public interface MyPhonebookLocal {
	public PhoneBook findByPrimaryKey(String name);
}

...

  1. @Stateless - tells Geronimo that this is a stateless session bean
  2. @PersistenceUnit - tells Geronimo to retrieve a persistence unit defined in the persistence.xml and place it in the EntityManagerFactory
    Note

    Note that PersistenceContext is used when you are directly obtaining a EntityManager. For an EntityManagerFactory use PersistenceUnit.

java
Code Block
java
borderStylesolid
titleMyPhonebookBean.java
java
package org.apache.geronimo.samples.myphonebookpak;

import javax.ejb.Stateless;
import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.apache.geronimo.samples.myphonebookpak.PhoneBook;

@Stateless
public class MyPhonebookBean implements MyPhonebookLocal {

	@PersistenceUnit(unitName="PhonePU")
	protected EntityManagerFactory emf;

	public MyPhonebookBean() {

	}

	public PhoneBook findByPrimaryKey(String name) {
		EntityManager em = emf.createEntityManager();

		PhoneBook phonebook = (PhoneBook)em.find(PhoneBook.class, name);

		em.close();

		return phonebook;
	}
}

index.jsp is the JSP page that uses the EJB to access the database.

Code Block
javajava
borderStylesolid
titleindex.jsp
java
<%@ page contentType="text/html" import="org.apache.geronimo.samples.myphonebookpak.*, javax.naming.* " %>

<%
	String searchName = "";
	if (request.getParameter("searchname") != null) {
		searchName=request.getParameter("searchname");
	}
%>

<html><head><title>Phonebook</title></head><body>
<form action="index.jsp">
<b>Search number</b>:<br>
Enter name: <input type="text" name="searchname" value="<%=searchName%>">
<input type="submit" value="Search">
(Test with <a href="index.jsp?searchname=Joe">Joe</a>)
</form>
<%
	if (! searchName.equals("")) {
		String number="";
		try {
			Context context = new InitialContext();
			MyPhonebookLocal myPhonebookLocal = (MyPhonebookLocal)context.lookup("java:comp/env/ejb/MyPhonebookBean");
			PhoneBook phonebook = myPhonebookLocal.findByPrimaryKey(searchName);
			if(phonebook != null) {
				number =  phonebook.getNumber();
			}
		}
		catch (Exception e) {
			number=e.toString();
		}
		out.println("This is the number returned from the EJB when searching for '"+searchName+"' : " + number);
	}
%>
</body></html>

...

openejb-jar.xml just specifies the module's information.

xml
Code Block
xml
borderStylesolid
titleopenejb-jar.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar
		xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
		xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
		xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0"
		xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
		xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
	<sys:environment>
		<sys:moduleId>
			<sys:groupId>org.apache.geronimo.samples</sys:groupId>
			<sys:artifactId>MyPhonebookBean</sys:artifactId>
			<sys:version>1.0</sys:version>
			<sys:type>car</sys:type>
		</sys:moduleId>
	</sys:environment>
</openejb-jar>

...

SEE BELOW FOR POSSIBLE SOLUTION

xml
Code Block
xml
borderStylesolid
titlepersistence.xml
xml
<?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="PhonePU">
		<description>Phone Book</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<class>org.apache.geronimo.samples.myphonebookpak.PhoneBook</class>
	         <jta-data-source>PhoneBookPool</jta-data-source>
	         <non-jta-data-source>PhoneBookPool</non-jta-data-source>
	</persistence-unit>
</persistence>

...

web.xml references the EJB by specifying the package to which the MyPhonebookLocal belongs to.

Code Block
xmlxml
borderStylesolid
titleweb.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    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">
	<display-name>MyPhonebookWeb</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<ejb-local-ref>
		<ejb-ref-name>ejb/MyPhonebookBean</ejb-ref-name>
		<ejb-ref-type>Entity</ejb-ref-type>
		<local>org.apache.geronimo.samples.myphonebookpak.MyPhonebookLocal</local>
	</ejb-local-ref>
</web-app>

geronimo-web.xml specifies the module's information and the context-root for the web-app.

xml
Code Block
xml
borderStylesolid
titlegeronimo-web.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1"
         xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
         xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
         xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
  <sys:environment>
    <sys:moduleId>
      <sys:groupId>${pom.groupId}</sys:groupId>
      <sys:artifactId>${pom.artifactId}</sys:artifactId>
      <sys:version>${version}</sys:version>
      <sys:type>war</sys:type>
    </sys:moduleId>
  </sys:environment>
  <context-root>/myphonebook</context-root>
</web-app>

...

geronimo-application.xml tells the application that there is a database pool that needs to be deployed as well. The db pool is defined in PhoneBookPool.xml and the driver that is needs in order to be deployed is the tranql-connector-ra-1.3.rar file--these two files will reside on the top level layer of the resultant EAR file.

xml
Code Block
xml
borderStylesolid
titlegeronimo-application.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<application	xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.1"
				xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
				xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1"
				application-name="t6">
    <sys:environment>
        <sys:moduleId>
            <sys:groupId>${pom.groupId}</sys:groupId>
            <sys:artifactId>${pom.artifactId}</sys:artifactId>
            <sys:version>${version}</sys:version>
            <sys:type>ear</sys:type>
        </sys:moduleId>
    </sys:environment>
    <module>
        <connector>tranql-connector-ra-1.3.rar</connector>
        <alt-dd>PhoneBookPool.xml</alt-dd>
    </module>
</application>

...

You can checkout the source code of this sample from SVN:

svn checkout http://svn.apache.org/repos/asf/geronimo/samples/trunk/samples/myphonebookImage Removed

Creating and Populating Database

After starting Apache Geronimo log into the console and follow the given steps to create the PhoneBookDB.

xml
Code Block
xml
borderStylesolid
titlePhoneBook.sql
xml
CREATE TABLE phonebook ( name VARCHAR(255) PRIMARY KEY, number VARCHAR(255) );
INSERT INTO phonebook VALUES ('John', '1234');
INSERT INTO phonebook VALUES ('Joe', '5678');

...

1. Include the module name in the app dependencies so geronimo knows to look in the new datasource module for the datasource. So, the openejb-jar.xml should look something like:

xml
Code Block
xml
borderStylesolid
titleopenejb-jar.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar
		xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
		xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
		xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0"
		xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
		xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
	<sys:environment>
		<sys:moduleId>
			<sys:groupId>org.apache.geronimo.samples</sys:groupId>
			<sys:artifactId>MyPhonebookBean</sys:artifactId>
			<sys:version>1.0</sys:version>
			<sys:type>car</sys:type>
		</sys:moduleId>
                <sys:dependencies>
                    <sys:dependency>
			<sys:groupId>console.dbpool</sys:groupId>
			<sys:artifactId>MyDS</sys:artifactId>
			<sys:version>1.0</sys:version>
			<sys:type>rar</sys:type>
                    </sys:dependency>
                </sys:dependencies>
	</sys:environment>
</openejb-jar>

2. specify the name of the datasource in persistence.xml, something like

xml
Code Block
xml
borderStylesolid
titlepersistence.xml
xml
<?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="PhonePU">
		<description>Phone Book</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
                <jta-datasource>MyDS</jta-datasource>
		<class>org.apache.geronimo.samples.myphonebookpak.PhoneBook</class>
		<properties>
			<property name="openjpa.jdbc.SynchronizeMappings" value="false" />
		</properties>
	</persistence-unit>
	<!--
	<jta-data-source>PhoneBookPool</jta-data-source>
	<non-jta-data-source>NoTXPhoneBookPool</non-jta-data-source>
	-->
</persistence>