Creating the Demonstration classes

The City entity and its supporting classes will provide methods which will illustrate the use of transactions. A session facade is created for the class along with a servlet and a PopulationManager class. This class along with the session facade is where we will find most of the transactions used in this chapter.

Getting ready

In this recipe, we will create a series of classes to support the illustration of transaction processing. These classes include:

  • City An entity class representing a city
  • CityFacade A facade class supporting the City entity
  • AbstractFacade The base class for CityFacade
  • PopulationServlet A servlet to drive the application

Subsequent recipes will build upon these classes.

How to do it...

Create a new Java EE application called PopulationApplication with an EJB and a WAR module. Add a packt package to the EJB module and a servlet package to the WAR module. Create the City entity in the packt package and add instance variables for its ID, name, country, and population. While not shown below, add getter and setter methods for the instance variables. Add a default and a three argument constructor.

@Entity
public class City implements Serializable {
private String name;
private String country;
private long population;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public City() {
}
public City(String name, String country, long population) {
this.name = name;
this.country = country;
this.population = population;
}

Next, create a CityFacade class based on the AbstractFacade class developed in Chapter 4, Creating an entity facade recipe.

@Stateful
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CityFacade extends AbstractFacade<City> {
@PersistenceContext(unitName = "PopulationApplication-ejbPU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public void create(City entity) {
getEntityManager().persist(entity);
}
public CityFacade() {
super(City.class);
}
}

Add a changePopulation method to this class. The method is passed the name of a city and a population. Within the method, add a println statement to reflect the progress through the method and a query to modify the population. The use of the Query object and the Java Persistence Query Language (JPQL) is covered in Chapter 5, Using the Update query recipe:.

public void changePopulation(String cityName, long count) throws IllegalPopulationException {
System.out.println("Executing changePopulation");
Query query = em.createQuery( "UPDATE City c " + "SET c.population = c.population+:count " + "WHERE c.name = :cityName");
query.setParameter("count", count);
query.setParameter("cityName", cityName);
int result = query.executeUpdate();
System.out.println("result: " + result);
System.out.println("--- end changePopulation");
}

Next, add a PopulationServlet to a servlet package in the WAR module. It should appear similar to the following:

public class PopulationServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet PopulationServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

Execute the servlet as illustrated in the following screenshot. Notice, there is no output for the current servlet. Subsequent recipes will provide output.

How to do it...

That's it for the moment. In the next recipe, we will augment these classes to support transactions.

How it works...

The City entity was used to hold information about a city. The CityFacade used the changePopulation method to update the population for a city. It was passed the name of the city and a count value. A new query was created using the createQuery method that contained a JPQL UPDATE query command. Named parameters, as detailed in Chapter 5, Using parameters in a query recipe, was used to specify the city to be changed. In this query, count was simply added to the city's population.

The PopulationServlet does not display anything at this point. In later recipes, we will modify the servlet display population information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.211