|Home > Documentation > Sample applications > Very simple Entity EJB example|
This is an example of a JSP-page calling an Enity Bean that uses annotations. The result looks like this:
First, let us take a look at the PhoneBook Entity Bean that represents a table in the database. Each instance of PhoneBook is a record of the table.
PhoneBook.java uses the
And, as usual their is an empty constructor for the Entity Bean
MyPhonebookLocal.java is the business interface that drives the above mentioned Entity Bean.
MyPhonebookBean.java is where the implementation of the local (and if there is, a the remote) interface. To explain what the annotations in this Stateless Session Bean means I will enumerate them:
Note that PersistenceContext is used when you are directly obtaining a EntityManager. For an EntityManagerFactory use PersistenceUnit.
index.jsp is the JSP page that uses the EJB to access the database.
openejb-jar.xml just specifies the module's information.
persistence.xml will specify the name of the PersistenceUnit. This name is used when referencing for the EntityManagerFactory. I have denoted it as PhonePU. For some reason I could not get it to reference with jta-data-source. So the alternative method is to explicitly specify the ConnectionURL, ConnectionDriverName, and ConnectionUserName. I added an extra property called SynchronizeMappings so that the data in the database will not be overwritten.
SEE BELOW FOR POSSIBLE SOLUTION
web.xml references the EJB by specifying the package to which the MyPhonebookLocal belongs to.
geronimo-web.xml specifies the module's information and the context-root for the 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.
Download the MyPhoneBook application from the following link:
After decompressing the given file, the myphonebook directory will be created.
You can checkout the source code of this sample from SVN:
After starting Apache Geronimo log into the console and follow the given steps to create the PhoneBookDB.
Use a command prompt to navigate into the myphonebook directory and just give mvn install followed by mvn site command to build. It will create the myphonebook-ear-2.0-SNAPSHOT.ear under the myphonebook folder. Now, you are ready to deploy myphonebook application in the Geronimo Application server.
Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.
To test the sample web application open a browser and type http://localhost:8080/myphonebook.
This app does not use openjpa sequences so apparently you can get by with only a jta-datasource. In my experience apps that do use openjpa sequences to supply primary key values also need a non-jta-datasource. When deploying such a non-jta-datasource check the plan and make sure it says <no-transaction/> rather than <local-transaction/> or <xa-transaction/>.
For this app, use the console to deploy a datasource using the database of your choice. You need to supply a name such as "MyDS" for your datasource. At the end you should have a name for the module your datasource is in such as console.dbpool/MyDS/1.0/rar. You need to do two things so geronimo can hook up to your datasource:
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:
2. specify the name of the datasource in persistence.xml, something like