JsfCrudGenerator

Basics

The JSF CRUD Generator (a.k.a. JSF Pages from Entity Classes wizard) has been resurrected in NB6.1 and significantly improved.

Evangelists typically demonstrate this feature by first generating entity classes from a database using the Entity Classes from Database wizard and then running the JSF Pages from Entity Classes wizard on the generated entities.

For information on the consulting agency database for use with NetBeans 6.1, see JsfCrudGeneratorConsultingAgency.

For information on the JSF CRUD Generator in NetBeans 6.5, see JsfCrudGenerator65.

For information on the Timecard application, which extends the consulting agency use case, see JsfCrudGenerator65Timecard.

For information on post-6.5 planning, see JsfCrudGeneratorAfter65Planning.

For information on the JSF CRUD Generator in NetBeans 7.0, see JsfCrudGenerator70.

Test Case Goals

In order to support evangelism, I am placing a high priority on test cases wherein the user generates entity classes from a database and then generates a JavaServer Faces application from the entities.

Furthermore, an ideal test case might include the following:

  1. It would have to be a convincing "real-world" case. Real enough to convince people during evangelist demos and in a related tutorial.
  2. All three kinds of relationships would be present.
  3. Some of the entities would have multiple relationship fields.
  4. One or more of the entities would have a composite primary key.
  5. It should work with generated or non-generated Ids.
  6. The property names of an entity would be different from the property type. For example, a property of type Donkey would be named something like bestDonkey rather than just donkey. Similarly, a Collection of Donkeys would be named something like donkeyCollection rather than donkeys. This is important because there were previously bugs in this area.
  7. It would work when nullable=false is annotated on columns that are non-nullable in the database (or, alternatively, when optional=false is annotated on the corresponding relationship fields), generating required checks for the corresponding input fields in the New.jsp and Edit.jsp pages.

Test Cases

So far, we have identified two databases that should prove useful for testing.

Sakila

In the spirit of presenting an example that is similar to a real-world scenario, as described by test goal (1), we recommend generating the entity classes from the sakila database, a MySQL example database. For information on downloading and installing sakila, see the MySQL Examples.

Attached to this page is sakila_JsfCrudGenerator.zip file containing entity classes that I generated from the sakila database schema and subsequently manually edited as necessary. In particular, I added @GeneratedValue annotations and optional=false on relationship fields that correspond to non-nullable database columns. In addition, because sakila includes a last_update column on its tables, including the cross reference tables film_actor and film_category, the Entity Classes from Database wizard does not generate any @ManyToMany annotations among the entity classes. (This is correct behavior on the part of that wizard.) However, since the purpose of those cross reference tables is to implement many-to-many relationships, I have edited the Film, Actor, and Category entities in the sakila_JsfCrudGenerator.zip file and removed the FilmActor and FilmCategory entities from that zip file in order to implement many-to-many relationships effectively between the Film and Actor entities as well as between the Film and Category entities.

To test the JSF CRUD Generator against the sakila database, please follow these steps:

  1. Install and start MySQL if necessary, according to instructions provided on the MySQL web site.
  2. Install the sakila database, according to instructions provided on the MySQL Examples.
  3. Open the Sakila project.
  4. Generate a JavaServer Faces application by selecting File > New File, and then selecting Persistence > JSF Pages from Entity Classes. Complete the wizard.

In my experience, the resulting JavaServer Faces application works well, though some of the New.jsp and Edit.jsp pages can be slow to load, presumably due to the numerous options populating some of the listboxes and dropdowns. I am uncertain whether there is any way I can improve the loading time of these pages.

Consulting Agency

While sakila provides an excellent pseudo real-world use case, it does not meet all the test case goals. In particular, after manually editing the entity classes generated from sakila to include @ManyToMany annotations between the Film and Actor entities as well as between the Film and Category entities, no entity has a composite primary key. Therefore, the modified sakila entities do not meet test goal (4).

Consequently, I have designed a database that does meet all the test case goals, except perhaps for (1). The database is called the consulting agency database. It is designed for Java DB (Derby) in order to facilitate easy installation.

After generating entities from the consulting agency database, it is necessary to manually edit the entities to add @GeneratedValue annotations, nullable=false on @JoinColumn annotations (or, alternatively, optional=false on the corresponding relationship fields), and to modify the relationship between Recruiters and Clients to be one-to-one rather than many-to-one. The last of these is useful for testing a one-to-one relationship where the foreign key (consult_recruiter_fk_client_name) comprises columns that are nullable, which is not possible without manually editing the entities.

Attached to this page is a consultingagency_JsfCrudGenerator.zip file containing the consult.sql script for creating the consulting agency database schema as well as the ConsultingAgency project. The ConsultingAgency project contains entity classes that were generated from the consulting agency database schema and subsequently manually edited as necessary.

To test the JSF CRUD Generator against the consulting agency database, please follow these steps:

  1. In the Services window, expand Databases, right-click on Java DB, and select Create Database.
  2. Enter consult as the Database Name, User Name, and Password in the Create Java DB dialog, and click OK. A new node under Databases appears (jdbc:derby://localhost:1527/consult).
  3. Right-click on the new node, and select Connect.
  4. Under the new node, right-click on the Tables node, and select Execute Command. A SQL Command editing window appears.
  5. In the SQL Command editing window, paste the contents of the consult.sql script, starting with the line to create the consult.address table ("CREATE TABLE consult.address"). Note: Because the tables have never been created before, it is necessary to exclude the commands at the top of the script that drop the constraints and tables, in order to avoid errors.
  6. In the SQL Command editing window, click the Run SQL toolbar button. No errors should result.
  7. Open the ConsultingAgency project.
  8. Generate a JavaServer Faces application by selecting File > New File, and then selecting Persistence > JSF Pages from Entity Classes. Complete the wizard.

For more information on exercising the JSF CRUD Generator against the consulting agency database, see JsfCrudGeneratorConsultingAgency.

Developer Tests

Details on tests I am using in my development can be found at JsfCrudGeneratorDevTests.

Jukebox Exercise

For information on the Jukebox exercise, see JsfCrudGenerator61Jukebox.

Adding Style to Generated Pages

Attached is a stylesheets_JsfCrudGenerator.zip file containing two stylesheets, jsfcrud.css and jsfcrudlist.css. The New.jsp, Edit.jsp, Detail.jsp, and welcomeJSF.jsp pages use jsfcrud.css and the List.jsp page uses jsfcrudlist.css. Note: We may want to modify the contents of these stylesheets before including them in online tutorials.

To apply these stylesheets to generated JavaServer Faces pages, please follow these steps. Note: The following steps presume you have selected the web folder as the JSF Pages Folder when you generated the JavaServer Faces pages (such that the web folder has, for instance, an address child folder containing its four JSPs). Make appropriate adjustments if you selected a different JSF Pages Folder.

  1. Generate JSF Pages from Entity Classes, leaving the JSF Pages Folder field blank so that the JSF Pages Folder will be the web folder.
  2. Drop jsfcrud.css and jsfcrudlist.css in the web folder. You can achieve this outside the IDE, or, alternatively, create the two stylesheets within the IDE.
  3. From the Edit menu, select Replace in Projects. Enter <head> in the Containing Text field, <head><link rel="stylesheet" type="text/css" href="../jsfcrud.css" /> in the Replace With field, and New.jsp, Edit.jsp, Detail.jsp in the File Name Patterns field. Make an appropriate selection under Scope and select Case Sensitive under Options. Click Find. Review the results and click Replace.
  4. From the Edit menu, select Replace in Projects. Enter <head> in the Containing Text field, <head><link rel="stylesheet" type="text/css" href="../jsfcrudlist.css" /> in the Replace With field, and List.jsp in the File Name Patterns field. Make an appropriate selection under Scope and select Case Sensitive under Options. Click Find. Review the results and click Replace.
  5. From the Edit menu, select Replace in Projects. Enter <head> in the Containing Text field, <head><link rel="stylesheet" type="text/css" href="./jsfcrud.css" /> in the Replace With field, and welcomeJSF.jsp in the File Name Patterns field. Make an appropriate selection under Scope and select Case Sensitive under Options. Click Find. Review the results and click Replace.
  6. Run the application.


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo