Simple CRUD Example – Part 2

In the last article I showed you how to create a Debry database and set up a persistence unit. In this article I’ll create a very basic CRUD application. Using JSF 2, JPA 2 and EJB 3.1.

At this point you should have a standard Maven web application with the only modification being the persistence.xml file we created in the previous article. The next step is to create some additional settings files that pretty much all web applications need.

Creating the Settings Files

With the advent of Java EE 6 most of the settings files have become optional but in my experience they are always necessary even if you actually put very little in them. It’s disappointing to see that Maven doesn’t create them for you any more.

The web.xml File

The first one to create is the web.xml file which lives under the WEB-INF directory. This file contains settings for the web application such as context parameters and listeners for server state etc. It also controls security, filters and a whole host of other things that we won’t be usign here. The default Maven web application doesn’t (for some unknown reason) configure your application to use JSF from the start so right click on the application node and select ​Properties​. Under the frameworks section click Add and select JavaServer Faces version 2.0 (the version used by the application server). The ​web.xml​ file should be created automatically along with a file called index.xhtml.

​The glassfish-web.xml File

You should also create the glassfish-web.xml ​file while you are here. This is easily done by selecting the ​WEB-INF​ directory. Right clicking and selecting New then Other. Scroll down the left list until you see GlassFish and then create a new GlassFish Descriptor.

The beans.xml File

Finally create the ​beans.xml​ file which tells the system that you will be using CDI. This file can be completely empty but it will trigger a warning. It is better to populate it as shown here.

You now have a fully set up web application.

The Front End

At this point I’m going to break with tradition with this type of tutorial and tell you to install PrimeFaces 3 (or later). The reason for doing this is simple, you won’t be using the basic JSF components in your applications – they are very limited – you’ll be using some other widget library and PrimeFaces is probably about the best out there at the moment. It’s also no more complex to use PrimeFaces than it is to use the basic widgets so there is no good reason to limit your options. Right click on the Dependencies node of your application and add PrimeFaces 3, you may need to set up your Maven installation so that it can locate the PrimeFaces repository as currently they aren’t available in the usual locations.

At a minimum we need to create four more files to make this a crud application. Three of the files are .java and one is .xhtml which contains JSF markup. We’ll start with the JSF page that will display the entities we create and let us modify them. I’m not planning on straying too far from the well beaten path of making an application that modifies People objects – sorry.

Open the index.xhtml file and enter the following contents:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
        <title>CRUD Example</title>
			<p:dataTable var="p" value="#{personView.people}">
					<f:facet name="header">Name</f:facet>

The Back End

Now create the following class files.

package wobblycogs.crudexample;

import javax.persistence.Entity;
import javax.persistence.Id;

public class Person implements Serializable {

	private Long id;
	private String name;

	public Person() {

	public String getName() {
		return name;

	public void setName(String name) { = name;
	public String toString() {
		return name;

	public Long getId() {
		return id;

	public void setId(Long id) { = id;

package wobblycogs.crudexample;

import java.util.List;
import javax.enterprise.context.RequestScoped;

public class PersonView {
	private List<Person> people;
	private Person selected;
	public PersonView() {

	public List<Person> getPeople() {
		return people;

	public void setPeople(List<Person> people) {
		this.people = people;

	public Person getSelected() {
		return selected;

	public void setSelected(Person selected) {
		this.selected = selected;

package wobblycogs.crudexample;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class PersonService {
	private EntityManager em;

	public PersonService() {

The Person class is the entity that the system will be manipulating. The PersonView is a CDI Bean that is used to handle the front end interaction and the PersonService is a stateless EJB used to control the business logic.

At this point if you fire the application up you’ll see that it automatically creates tables in the database and if you visit the index.xhtml page you’ll see an empty table. In the next article I’ll get the application doing something interesting.