Show TOC

Procedure documentationAccessing Persistence in Web Applications Locate this document in the navigation structure

 

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

Recommendation 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).

End of the recommendation.

Procedure

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

    More information: Creating JPA Projects in the Developer Studio

    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.

    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.

Example Example

  1. 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);
       }
    
    //…
    
    }
    
End of the code.

Example Example

  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name ="PUnit"/>
    </persistence>
    
End of the code.
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.

Example Example

  1. 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);
    
    //…
    }
    
End of the code.

Example Example

  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name ="PUnit"/>
    </persistence>
    
End of the code.