Application-managed persistence is useful when special handling of persistence is required such as the explicit management of an EntityManager
life cycle or more explicit control over Java Transaction API (JTA) transactions is needed. Application-managed persistence is the focus of this recipe.
The essential process for using application-managed persistence involves:
EntityManagerFactory
to provide a means to create an EntityManager
EntityManager
to control the persistence process EntityManager
method to manage the entityCreate a new Java EE application called ApplicationManagedPersistenceApplication
. In this application we will only use the WAR module. Add a packt
package and a servlet
package to the module. Add a RegistrationBean
to the packt
package and a RegistrationServlet
to the servlet package.
We will use a RegistrationBean
to illustrate application-managed persistence. This entity represents a registration for some event and includes fields for:
name
The name of the participantcompany
The name of the participant's companysession
An integer representing the session to be attendedAdd the RegistrationBean
along with an id
field and getters and setters.
@Entity public class RegistrationBean implements Serializable { private String name; private String company; private int session; public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSession() { return session; } public void setSession(int session) { this.session = session; } private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; public Long getId() { return id; } ... }
Next add the RegistrationServlet
as shown below. The doGet
and doPost
methods are not shown.
public class RegistrationServlet extends HttpServlet { @PersistenceUnit(unitName = "ApplicationManagedPersistenceApplication-warPU") EntityManagerFactory entityManagerFactory; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RegistrationBean registration; RegistrationBean secondRegistration; EntityManager entityManager; response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { try { registration = new RegistrationBean(); registration.setName("Steve Best"); registration.setCompany("Grey Beard Software"); registration.setSession(10); entityManager = entityManagerFactory.createEntityManager(); entityManager.persist(registration); secondRegistration = entityManager.find( RegistrationBean.class, registration.getId()); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet RegistrationServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>" + secondRegistration.getName() + " ID: " + secondRegistration.getId()+ "</h1>"); out.println("</body>"); out.println("</html>"); entityManager.close(); } catch (SecurityException ex) { Logger.getLogger(RegistrationServlet.class.getName()). log(Level.SEVERE, null, ex); } catch (IllegalStateException ex) { Logger.getLogger(RegistrationServlet.class.getName()). log(Level.SEVERE, null, ex); } } finally { out.close(); } }
Next, execute the application using the URL shown in the following screenshot:
The RegistrationServlet
began with the insertion of the EntityManagerFactory
object as a persistence unit. Since we are executing as a client, an EntityManagerFactory
is needed to create the EntityManager
.
In this example, two RegistrationBeans
variables were defined: registration
and secondRegistration
. Next, a RegistrationBean
was created and its fields were assigned values for a name, company, and a session. The EntityManagerFactory
class's createEntityManager
method created an instance of an EntityManager
. The EntityManager
was used to persist the RegistrationBean
.
In order to verify the entity was actually saved, secondRegistration
was used to hold a reference to the entity retrieved from the table. The secondRegistration
variable's name
and id
fields were then displayed. The close
method was used to close the EntityManager
as it was not longer needed. This is a good practice as it helps free up database connections.
With application-managed persistence, the management of transactions is delegated to the application. In this example we did not use transactions. However, this topic is addressed in Chapter 6,
3.143.203.96