Show TOC Start of Content Area

Procedure documentation Accessing Persistence in Web Applications  Locate the document in its SAP Library structure

Use

Use this procedure to access the persistence tier of Java EE 5 applications from the Web tier.

Recommendation

We recommend that you use the EJB tier as an intermediary for accessing persistence instead of working directly with persistence from the Web tier. The EJB tier handles all persistence operations, and the Web tier works with entities as POJOs (pure old Java objects).

Procedure

...

       1.      In the Dynamic Web Project wrapping your application, add the Java Persistence facet.

       If you are creating the project now, choose the Java Persistence facet in the Project Facets page of the new project wizard.

       If the project is already created, add the Java Persistence facet using PropertiesProject Facets (in the context menu).

Note

After adding the Java Persistence facet, the Developer Studio prompts you to specify database connection settings. If you want to make automatic forward or backward object/relational mapping between entities and database tables, you need to specify these settings. If you do not need this function, you may skip specifying these settings.

More information about database settings: Creating Database Connections.

This step creates a persistence.xml file in the project, and enables the project to relate to AS Java’s database.

       2.      In the persistence.xml, describe the persistence unit.

       3.      In the source code, obtain an entity manager directly, or indirectly, using the entity manager factory.

More information: Obtaining an Entity Manager Instance.

Characteristic

When obtaining an entity manager for servlets, take into account the thread-safety issue. A servlet may serve multiple concurrent requests using multiple threads. So all instance variables are shared by all these threads, which may cause undesired side-effects. Methods in EntityManager interface are not thread-safe, and may not be shared among multiple concurrent requests. Therefore, do not inject EntityManager into a servlet instance variable, or at least try to avoid it.

In servlet components, we recommend using an entity manager factory to obtain an entity manager instance. All methods of EntityManagerFactory are thread-safe.

       4.      Using the entity manager instance, manage entities.

More information: Managing Entity Instances.

Example

Getting an Entity Manager Using EntityManagerFactory

The following example shows a servlet getting an entity manager using EntityManagerFactory. The EntityManagerFactory is injected by using the @PersistenceUnit (unitName = “PUnit”) annotation. The persistence unit with the specified name must be defined in the persistence.xml in the application archive. If only one persistence unit is defined, the unitName attribute is optional.

public class PU_annotTestServlet extends HttpServlet {

 

    @PersistenceUnit (unitName = "PUnit")

    private EntityManagerFactory emf;

 

    public void doGet ( HttpServletRequest req ,  HttpServletResponse resp){

 

         //Create an EntityManager to communicate with entities(tables).

      EntityManager em = emf.createEntityManager();

 

      //…

      String name = req.getParameter("name");

 

         // Search a member with a specified name in UserCredential table

         UserCredential credential = em.find(UserCredential.class, name);

   }

 

//…

   

}

 

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

    <persistence-unit name ="PUnit"/>

</persistence>

Getting an Entity Manager Using Persistence Context

The following example illustrates a JSF managed bean injecting the entity manager instance using @PersistenceContext (unitName = “PUnit”). Like in the previous example, you need to describe the persistence unit with name PUnit in the persistence.xml in the WAR.

public class PC_annotTestMBean {

 

 

   @PersistenceContext(unitName="PUnit")

   private EntityManager em;

 

//…

String name = req.getParameter("name");

 

   // Search a member with a specified name in UserCredential table

         UserCredential credential = em.find(UserCredential.class, name);

 

//…

}

 

 

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

    <persistence-unit name ="PUnit"/>

</persistence>

 

End of Content Area