Generating JSF Applications from JPA Entities

Exclusive offer: get 50% off this eBook here
Java EE 6 Development with NetBeans 7

Java EE 6 Development with NetBeans 7 — Save 50%

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

$29.99    $15.00
by David R. Heffelfinger | June 2011 | Java Open Source

One very nice feature of NetBeans is that it allows us to generate JSF applications that will perform Create, Read, Update, and Delete (CRUD) operations from existing JPA entities. This feature, combined with the ability to create JPA entities from an existing database schema as described in the previous section, allows us to write web applications that interact with a database in record time.

In this article by David R. Heffelfinger, author of Java EE 6 Development with NetBeans 7, we will see how to generate JSF applications from JPA entities.

 

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.

Summary

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


Further resources on this subject:


Java EE 6 Development with NetBeans 7 Develop professional enterprise Java EE applications quickly and easily with this popular IDE
Published: June 2011
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

About the Author :


David R. Heffelfinger

David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing and developing software professionally since 1995 and has been using Java as his primary programming language since 1996. He has worked on many large scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He has a Masters degree in Software Engineering from Southern Methodist University. David is editor in chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.

Books From Packt


NetBeans IDE 7 Cookbook
NetBeans IDE 7 Cookbook

Java EE 6 with GlassFish 3 Application Server
Java EE 6 with GlassFish 3 Application Server

Learning Ext JS 3.2
Learning Ext JS 3.2

Django JavaScript Integration: AJAX and jQuery
Django JavaScript Integration: AJAX and jQuery

Apache Wicket Cookbook
Apache Wicket Cookbook

BlackBerry Java Application Development
BlackBerry Java Application Development

jQuery 1.4 Reference Guide
jQuery 1.4 Reference Guide

Learning jQuery 1.3
Learning jQuery 1.3


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
G
t
4
Y
i
7
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software