Java EE 6 Development with NetBeans 7 — Save 50%
Develop professional enterprise Java EE applications quickly and easily with this popular IDE
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.
|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.
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.
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.
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.
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.
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:
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.
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.
We can see our newly created customer by clicking on Show All Customer Items.
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.
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.
- Java Refactoring in NetBeans [Article]
- NetBeans IDE 7: Building an EJB Application [Article]
- Java Data Objects and Service Data Objects in SOA [Article]
|Develop professional enterprise Java EE applications quickly and easily with this popular IDE|
eBook Price: $29.99
Book Price: $49.99
About the Author :
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.