Chapter 8. Generate a CRUD Database Application

8.1. What is CRUD?

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 chapter we will create Seam Web application, connect it to the PostgreSQL database and add CRUD support.

8.2. How to create the CRUD Database Application with Seam

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

Seam Web Project Creation

Figure 8.1. Seam Web Project Creation


  • Name your project as crudapp and follow the next wizard steps keeping default settings.

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

New Seam Project Wizard

Figure 8.2. New Seam Project Wizard


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

Seam Facet page

Figure 8.3. Seam Facet page


  • On New Connection Profile dialog select the PostgreSQL JDBC Connection type and click Next.

New JDBC Connection Profile

Figure 8.4. New JDBC Connection Profile


  • Name your profile as cruddb and press Next.

New JDBC Connection Profile Name

Figure 8.5. New JDBC Connection Profile Name


  • On the next page click ... to select a database driver.

New JDBC Connection Profile Database Driver

Figure 8.6. New JDBC Connection Profile Database Driver


  • On Driver Definitions dialog select the PostgreSQL->8.x and click Add....

Driver Definitions List

Figure 8.7. Driver Definitions List


  • Set the location of JDBC driver and fill all other necessary fields (URL, Database Name, Password, User Id) and click OK.

Edit Driver Definition

Figure 8.8. Edit Driver Definition


  • Click OK on the next dialog with newly created driver.

Driver Definitions List

Figure 8.9. Driver Definitions List


  • Click Test Connection on the next dialog and observe that connection can be established.

Test JDBC Connection

Figure 8.10. Test JDBC Connection


JDBC Connection is OK

Figure 8.11. JDBC Connection is OK


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

Validate JDBC Connection settings

Figure 8.12. Validate JDBC Connection settings


  • Observe that newly created profile has been selected on Seam Facet page and click Finish.

    This will create 2 projects - crudapp and crudapp-test.

Last Step of New Seam Project Wizard

Figure 8.13. Last Step of New Seam Project Wizard


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

CRUDAPP Seam Project

Figure 8.14. CRUDAPP Seam Project


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

Connecting to the CRUDDB database

Figure 8.15. Connecting to the CRUDDB database


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

CRUDDB Database

Figure 8.16. CRUDDB Database


  • 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. In the Generate Seam Entities dialog keep everything by default and press Finish.

Generate Seam Entities

Figure 8.17. Generate Seam Entities


  • Under WebContent folder you can find a lot of generated xhtml files:

Entities Web Pages

Figure 8.18. Entities Web Pages


  • And under src folder java classes are created.

Entities Java Classes

Figure 8.19. Entities Java Classes


  • 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.

Hibernate Configurations View

Figure 8.20. Hibernate Configurations View


  • Observe that Mapping Diagram is opened in the editor. Here you can see the relations between models and database tables. Select Customers entity model, right click and select Open Source File.

Mapping Diagram

Figure 8.21. Mapping Diagram


  • This will open the Customers.java file in the java editor.

Entity class from Mapping Diagram

Figure 8.22. Entity class from Mapping Diagram


After that you are ready to deploy your application to J2EE application server. This is described in the next chapter.