Generating JSF Applications from JPA Entities

Developing Java EE applications is faster and far less demanding when you can use NetBeans. The guide quickly familiarizes you with this popular Integrated Development Environment (IDE) through practical instruction.


Java EE 6 Development with NetBeans 7

Java EE 6 Development with NetBeans 7

Develop professional enterprise Java EE applications quickly and easily with this popular IDE

        Read more about this book      

(For more resources on Java, see here.)

To generate JSF pages from existing JPA entities, we need to right-click on the project, select File | New File, then select the JavaServer Faces category and the JSF Pages from Entity Classes file type.

In order for us to be able to generate JSF pages from existing JPA entities, the current project must be a Web Application project.

Java EE 6 Development with NetBeans 7

After clicking on Next>, we need to select one or more JPA entities. We would typically want to select all of them, they can easily be selected by clicking on the Add All>> button.

Java EE 6 Development with NetBeans 7

The next page in the wizard allows us to specify a package for newly created JSF managed beans. Two types of classes are generated by the wizard, JPA Controllers and JSF Classes, we can specify packages for both of these individually.

Java EE 6 Development with NetBeans 7

We are also given the opportunity to specify a folder for the JSF pages to be created, if we leave this field blank, pages will be created in our project's Web Pages folder.

The value of the Session Bean Package and JSF Classes Package text fields default to the package where our JPA entities reside. It is a good idea to modify this default since placing the JSF managed beans in a different package separates the data access layer classes from the user interface and controller layers of our application. After clicking on Finish, a complete web application that can perform CRUD operations will be created.

Java EE 6 Development with NetBeans 7

As we can see, NetBeans generates a folder for each of our entities under the Web Pages folder of our application. Each of the folders has a Detail, Edit, List, and New XHTML files. These files are JSF pages using Facelets as their view technology. The Detail page will display all properties for a JPA entity, the Edit page will allow users to update information for a specific entity, the List page will display all instances of a specific entity in the database, and the New page will provide functionality to create new entities.

The generated application is a standard JSF application. We can execute it by simply right-clicking on the project and selecting Run. At that point the usual things happen, the application server is started if it wasn't up already, the application is deployed, and a web browser window is opened displaying the welcome page for our application.

Java EE 6 Development with NetBeans 7

As we can see, the welcome page contains a link corresponding to each of our JPA entities. The links will display a table displaying all existing instances of our entity in the database. When we click on the Show All Customer Items, the following page is shown:

Java EE 6 Development with NetBeans 7

Since we haven't inserted any data to the database yet, the page displays the message (No Customer Items Found). We can insert a customer into the database by clicking on the Create New Customer link.

Java EE 6 Development with NetBeans 7

Notice how an input field is generated for each property in our entity, which in turn corresponds to a column in the database table.

As we can see, an input field was generated for the primary key field of our entity. This field is only generated if the JPA entity does not use a primary key generation strategy.

After entering some information on the page and clicking on the Save link, the data is saved, or the form is cleared and the message Customer was successfully created is shown.

Java EE 6 Development with NetBeans 7

We can see our newly created customer by clicking on Show All Customer Items.

Java EE 6 Development with NetBeans 7

At this point we can see our newly created customer in the list of customers on this JSP. Notice that the page has links to View, Edit, and Destroy (delete) the entity.

Let's say we would want to add an address for our customer, we could do so by clicking on the Index link, then clicking on Show All Address Items, then on New Address.

Java EE 6 Development with NetBeans 7

The Address entity is at the "one" end of several one-to-many relationships, notice how a combo box is generated for each one of the entities at the "many" end. Since we wish to assign this address to the customer we just added, we attempt to select a customer from the CustomerId combo box.

A better name could be used for the CustomerId field, the reason this is the label for the combo box is because it matches the property name on the Address JPA entity, which in turn could have a better name such as customer. Recall that all entities on this project were automatically generated from an existing database schema.

Clicking on the combo box reveals a cryptic, almost undecipherable (from the users' point of view anyway) label for our customer. The reason we see this label is because the labels generated for each item in the combo box come from the toString() method of the entities used to populate it. We can work around this issue by modifying the toString() method so that it returns a user-friendly String suitable to use as a label.

As we can see, the generated code from NetBeans wizards could certainly use some tweaking, such as modifying the toString() methods of each JPA entity so that it can be used as a label, modifying some of the property names on the entities so that they make more sense to us developers, modifying the labels on the generated JSF pages so that they are more user-friendly, and last but not least, the pages themselves are not very visually appealing. It would be a good idea to modify them so that they don't look so plain. Nevertheless, as we can see we can have a fully working application completely created by a few clicks of the mouse. This functionality certainly saves us a lot of time and effort.


In this article we saw how NetBeans can generate a complete JSF application from existing JPA entities.

Further resources on this subject:

Books to Consider

comments powered by Disqus

An Introduction to 3D Printing

Explore the future of manufacturing and design  - read our guide to 3d printing for free