Chapter 12. Generate a CRUD Database Application

12.1. What is CRUD?
12.2. How to create the CRUD Database Application with Seam

CRUD is an acronym for the four basic types of SQL commands: Create , Read , Update , Delete . Most applications have some kind of CRUD functionality, and we can assume that every programmer had to deal with CRUD at some point. A CRUD application is one that uses forms to get data into and out of a database.

In the next section we will create Seam Web application, connect it to the HSQL database and add CRUD support.

  • First, you should download a database and start it by running ./runDBServer.sh or runDBServer.bat from the database directory.

  • Create a new Seam Web Project using New Seam Project wizard or main menu File > New > Seam Web Project.


  • Name your project as crudapp, specify Target Runtime, Server and Seam Runtime configuration. Then press Next and follow the next wizard steps keeping default settings.


Tip:

Please have a look here how to create Target Runtime and Seam Runtime in order to get started creating, running, and debugging J2EE applications.

  • On Seam Facet page click New... next to the Connection profile section to create a new Connection profile.


  • On New Connection Profile dialog select the HSQLDB connection profile type, name it cruddb and click Next.


  • On the next page click the round icon next to the Drivers field to select a database driver.


  • Now you should see the New Driver Definition dialog. On the first tab select the HSQLDB JBDC Driver. Underneath in the Driver name field you can change its name if you need.


  • You may notice the note on the previous figure. It prompts that you should specify the driver of the type you pointed. Set the location of the driver by switching to the next tab and press Add JAR/Zip button.


  • On the Properties tab set the Connection URL, Database Name and User ID and click OK.


  • After clicking OK to submit the newly created driver you can observe and if you need edit all specified connection details.


  • Now click Test Connection to be sure that connection can be established.


  • Validate JDBC Connection profile settings and press Finish or Back if something is wrong.


  • After clicking Finish two projects crudapp and crudapp-test will be created.


  • Have a look at the created projects. You can expand WEB_CONTENT folder and open home.xhtml or login.xhtml with JBoss Visual Editor.


  • Switch to Database Development perspective with Window->Open Perspective->Other... and connect to the cruddb database.


  • Expand cruddb nodes to view its Schemas, Tables, Columns etc.


  • Switch back to the Seam perspective. From the toolbar select New->Seam Generate Entities to create a set of CRUD Seam components and web pages based on existing tables in the database. On the first page of Generate Seam Entities wizard keep everything by default and press Next.


  • Click Refresh to display all the tables from the database.


  • Now you can filter the tables. Use the Include button to include only necessary ones.


  • Under WebContent folder you can find all generated xhtml files:


  • And under src folder java classes are created.


  • Switch to Hibernate perspective with Window->Open Perspective->Other.... On Hibernate Configurations view expand the crudapp configuration. Right click on Customers and select Open Mapping Diagram from the popup menu.


  • Observe the Mapping Diagram opened in the editor. Here you can see the relations between models and database tables. For better navigating on the diagram use the Outline view.


  • For example, select Customers entity, right click and select Open Source File. This will open the Customers.java file in the java editor. You can also do this by right-clicking on the same entity on the Mapping Diagram.


So far, you are ready to deploy your application to JBoss Application Server. This is described in the next chapter.