THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
- Create a new Java project in Eclipse called, * "OpenJPATutorial".
- From the menu, select: File->New->Enterprise Application Project. (If Enterprise Application Project is not available as an option, select Project and then choose Enterprise Application Project from the list. Then click on the Next button).
When the New Project settings dialog appears, use the following settings:
- From the menu, select: File->New->Enterprise Application Project. (If Enterprise Application Project is not available as an option, select Project and then choose Enterprise Application Project from the list. Then click on the Next button).
- Under the Target Runtime section, select Apache Geronimo v2.2.
- If you do not already have Geronimo setup in Eclipse then you will have to do so now. Click on the New... button.
- If Apache Geronimo v2.2 does not appear in the list under Apache, click the Download additional server adapters link at the top right of the dialog. If the adapter does not appear in that list then follow the directions from the Geronimo site.
- Select Apache->Apache Geronimo v2.2
- Click Next.
- Set the JRE to jre6 if it is not already set.
- Browse for the install directory of Geronimo v2.2 on your system.
- Click Finish. You should see the following:
- Now, click the Next button. You should see this:
- Check the Generate application.xml deployment descriptor option.
- Click the New Module... button:
- De-select Create default modules.
- Select the Web option.
- Click Next.
- Click Finish. You will see the following:
- Click Finish.
- Now, your Project Explorer should look like this (partially expanded):
- If you double-click on the Deployment Descriptor: OpenJPATutorial, you should see the application.xml open:
- Now we will bring in the sample code. The easiest way to add the sample code is to find the source provided with this tutorial and copy it to the src folder under the OpenJPATutorialWeb folder in your project directory in Windows Explorer:
- Now go back to Eclipse. Right-click on the OpenJPATutorialWeb folder in the Project Explorer view and select Refresh, or press the F5 key on your keyboard. Now you will see this:
...
- Also in the InventoryEntityBroker is code to add entries the database tables. Since you did not create a table for InventoryItem in the StoreSystem database in Derby, OpenJPA 2.0 will create the table the first time an "add" is attempted. ** Consider the following code:
void addItem(String name, String description, float price, int categoryID)
{
InventoryItem item = new InventoryItem();
item.setName(name);
item.setDescription(description);
...
em.persist(item);
{color:black}}...
You can then call the addItem() method in another part of the code. The EntityManager.persist() method will throw an exception if a transaction has not been started. The transaction must be committed by calling the Transaction.commit() method after all updates have been applied or else the changes will not be saved to the database:
em.getTransaction().begin();
addItem(...);
em.getTransaction().commit();- * When this is executed the first time it will use the annotations to create the Person table+,+ then OpenJPA 2.0 will populate it with the provided data.
- Note the use of the getTransaction() method to start an update and then to commit it. The concept is the same as in any database transaction processing.
- Also note that while the getAllItems() function requires a JPQL SELECT query, the update type actions have APIs.
- Take a look in the InventoryEntityBroker code at the addItem(), updateItem() and deleteItem() functions and note the simplicity of these operations.
- * When this is executed the first time it will use the annotations to create the Person table+,+ then OpenJPA 2.0 will populate it with the provided data.
...