Persistence API Overview | Simple Persistence Example | Persistence Example with Query and Sort
This example uses the Persistence API to create, store, retrieve and query objects. One object has additional attributes and has a 1:1 relation to another object. The application is developed as servlet that loads a bean with the retrieved data of the Persistence API and calls the JSP that displays it.
This example is based on the Simple Persistence Example. In the simple Persistence example we are limited by the "Car" object. The example uses the "make" combined with the "model" of a car as entityKey. This allows to store only one car per "make"-"model" combination. To overcome this problem we define attributes to the "Car" object and store the "Car" object with an unique entityKey (implemented as incrementing counter). The changes take place in:
The example defines two objects that have a 1:1 relation. As typical 1:1 relation we haven chosen a "car" : "license"(number) scenario. The example demonstrates how the Persistence API has to be set up and how to read and write data. The MDK provides a template for this example. Click here to Download template.
The example uses following packages:
import com.sap.ip.me.api.conf.Configuration;
import com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServlet;
import com.sap.ip.me.api.services.Log;
import com.sap.ip.me.api.persist.*
Java - default package |
Description |
PersistenceExample.java | Implementation of the servlet. |
Constants.java | Interface defining all text strings used in PersistenceExample.java. |
Java - datafactory package |
Description |
Car.java | Implementation of the car object. |
License.java | Implementation of the license object. |
ExamplePackagePersistenceMaster.java | Implementation of the PackagePersistentMaster to define the data structure. |
ReadWriteEntities.java | Methods to add, read and delete entities. |
Java - bean package |
Description |
TableViewBean.java | Implementation of the bean that is loaded by the servlet and read by the JSP. |
JSP |
Description |
menu.jsp | Initial JSP to display the persisted data. |
query.jsp | JSP to enter a filter string. |
add.jsp | JSP to add an entry. |
delete.jsp | JSP to delete an entry. It serves as plane information page, in case the user clicks the DELETE button in the menu.jsp, without a selected row. |
As mentioned above, we create "Car" and "License" objects. One Car can have one License (number). In this implementation the "Car" and "License" must be unique. Whenever you add an entry that already exists, it replaces the existing entry with the new data.
The user interface of the initial screen has a display area and three buttons:
We create a class PersistenceExample that extends the class AbstractMEHttpServlet and implements Constants. Constants is an interface in which we define all relevant variables for the application. All variables used in Constants are written in uppercase characters. So anytime you see an uppercase variable used in class PersistenceExample you will find the definition in interface Constants.
The class PersistenceExample extends AbstractMEHttpServlet which extends javax.servlet.http.HttpServlet itself. The application is separated into the default package (containing all classes necessary to interact with the user), the datafactory (containing all classes necessary to manipulate the persisted data) and bean (which contains the bean which serves as data bag between the servlet and the JSP).
default package
Now comes the section were we check which event occurred. The events come from:
datafactory
package
to add the entries. If the user adds a car and a license number, both object
will be combined other car or license number will be stored independently.
The next methods called are:
datafactory
package. The parameters of the readEntity
method control, if all persisted elements are displayed or only entries
matching the filter string. The readEntity method returns an array
with the entries and the loadBean method transfers this data into
the bean.Now the JSP is called and the data is displayed on your web browser.
datafactory package
For an example about how to combine several conditions, see the ReadWriteEntities.java file form the AWT persistence example. If you want to use the DB based JQueryFactory, click here for a JQuery code snippet.
DeleteEntity
Deletes an entry with the given name.
The bean serves as data bag. It has a string, a vector and two variables that define the length and width of the string array. All variables have get and set methods.
menu.jsp
The JSP gets the bean from the session context with the useBean command.
It displays the the title and then uses the HTML grid (<td>, <tr>
...) to format the data in the bean. It uses a table view to display the persisted
data. The table view has a check box in the first row to mark entries to be
deleted. The JSP has three buttons to add and delete entries and to switch between
full list or short list.
The table view in this example has seven columns. Using standard fonts would
exceed the horizontal resolution of a PDA. To avoid that we did:
add.jsp
JSP with input fields and list boxes (to enter the car attributes and a license
number) and two buttons ("Add" to add the entry, "Cancel"
to go back to menu.jsp without adding the entry).
query.jsp
JSP with an input fields to define a filter string. The a filter string is applied
to the "make" attribute of a car. If you enter "BMW" as
filter string, only "BMW" cars are displayed in menu.jsp. The query
condition is made with the STARTSWITH option. If you leave the input field empy
and choose "OK", all entries are displayed. When the "Cancel"
button is clicked, the menu.jsp is displayed without any changes.
delete.jsp
If no entry is activated in menu.jsp and the user clicks on the "Delete"
button, an information message is displayed to select an entry first before
pressing the "Delete" button.