JBoss.orgCommunity Documentation

Chapter 13. Generate a CRUD Database Application

13.1. What is CRUD?
13.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 a Seam Web application, connect it to the HSQL database and add CRUD support.

  • First, you should download the employee-hsqlb database, extract the employee-hsqldb.zip 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 FileNewSeam 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 at Chapter 2, Creating a New Seam Project via the New Seam Project wizard 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 selecting hsqldb.jar from the list and clicking the Edit JAR/ZIP button.


  • On the Properties tab set the Connection URL to jdbc:hsqldb:hsql://localhost:1701, Database Name to employee and User ID to sa 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.


  • You can expand the WebContent folder of the crudapp project in the Project Explorer and open home.xhtml or login.xhtml with JBoss Visual Editor.


  • Switch to Database Development perspective with WindowOpen PerspectiveOther.

    Connect to the cruddb database if not already connected to it.


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


  • Switch back to the Seam perspective. From the toolbar select NewSeam 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, select Reverse engineer from database click press Next.


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


  • Now you can filter the tables. Use the Include button to include those that are necessary and click Finish.


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


  • And under src folder java classes are created.


  • Switch to Hibernate perspective with WindowOpen PerspectiveOther.... 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.