Tired of simple hello world tutorials or tutorials taking you only as far as user login and registration examples? Here’s an intermediate-level tutorial in which I will demonstrate how to make a blog CMS application using JBoss Seam incorporating JSF (w/Facelets), Hibernate JPA and how to deploy it to Apache Tomcat web server. With this tutorial, I also aim to provide complete and correct instructions for preparing a Seam 2.2 project to be deployed on Tomcat without errors.
The blog we’ll make in this tutorial is simple, but the tutorial itself is not.
Here’s a list of technologies and frameworks with their versions which we will use here:
- Java EE 5 (on JRE 6)
- JBoss Seam 2.2.2 (incorporating JSF 1.2 + Facelets, RichFaces 3.3.3 and Hibernate JPA 1.0)
- Apache Tomcat 6.0
- MySQL 5.1 (community edition)
And the tools we will use such as the IDE and the database GUI administration tool:
Tired of simple hello world tutorials? Here’s an intermediate-level tutorial in which we’re going to make a blog cms application using JBoss Seam incorporating JSF 1.2 + Facelets, Hibernate JPA 1.0 and we’ll deploy it to Tomcat.Tired of simple hello world tutorials? Here’s an intermediate-level tutorial in which we’re going to make a blog cms application using JBoss Seam incorporating JSF 1.2 + Facelets, Hibernate JPA 1.0 and we’ll deploy it to Tomcat.
Make sure you get the tools above or some equivalent so that you can follow me as I go along the tutorial. Although Seam development with JBoss Tools makes it easy to incorporate the necessary libraries (jars) into your project, in case of Tomcat development you will need to bring along your own set of JSF libraries – for the sake of the tuorial and making life easier I created a bundle of the necessary jar files in a zip file which you could download from here.
Creating the Blog Database
Assuming you already have a MySQL 5.1 database server installed and running, we will create the blog database on MySQL using Navicat and then the necessary tables.
In Navicat, having your localhost defined and connected to, right-click localhost and select ‘Create new database…’ and then you should be presented with a dialog box as shown in figure 1.1 asking you to specify the database name and select a character set from a drop-down menu.
Enter “seamblog” (without quotes) and select UTF-8 (unicode) from the pull-down menu below, then click OK. (Optionally you can specify a collation matching that of your language if it is anything different than English.
Then click ‘Manage users’ the big button with a user icon on the toolbar of Navicat’s main window whileyour newly created database is selected. Alternatively you can right-click ‘seamblog’ from the databases list and select ‘Manage Users…’ from the pop-up menu.
Name the user ‘blogger’, the host localhost and enter ‘12345’ as the password then click OK.
Click the tiny triangle (bullet) preceding the blogger@localhost user to display the database list, and select seamblog from the list, then Select All the priveleges on the right pane of the window as shown in figure 1.3. Finally click ‘save’.
Now it’s time we created the necessary tables.
Click ‘New Table’ from the the Navicat toolbar, then create three fields named ‘id’ (smallint, primary key, auto-incrementing), ‘name’ (varchar, 31 characters length) and version (smallint, with null values allowed) respectively. Then save the table with the name ‘category’. It’s easy with Navicat’s intuitive graphic user interface. An SQL dump of the database is also included at the end of the database section of the tutorial.
As the name suggests, this table is where the categories of blog posts will be kept. Now onto the ‘user’ table:
Create the user table with the following fields as shown in figure 1.5:
- id (mediumint, allow null, primary key with auto-increment)
- name (varchar 50)
- login (varchar 12)
- password (varchar 40, allow null)
- email (varchar 50)
- level (tinyint)
And finally the table for posts:
For this one we need the following fields as shown in figure 1.6:
- id (int, auto-incrementing primary key)
- user_id (mediumint, null values allowed)
- category_id (smallint)
- title (varchar 200)
- content (longtext)
- post_date (datetime)
- version (smallint, allowing null values)
Now that’s it for creation of the tables. Next, we will define some foreign-key restraints. This is absolutely necessary for mapping the many-to-one and one-to-many relationships between the JPA entities.
Now right-click the post table from the tables list and select ‘Design table’ from the pop-up menu. In the table design window, switch to the ‘Foreign Keys‘ tab. Then click ‘Add foreign key‘ button on the toolbar.
Name the foreign key ‘fk_post_4_user’, choose user_id for field name(s), seamblog for reference database, user for reference table, and check ‘id’ from the pop-up menu under ‘Foreign Field Names’ as shown in figure 1.7. Select cascade on delete, and restrict on update.
What this FK definition means is that for a post we have many users to choose from, and a user can have many posts.
Now we need one last foreign key for defining a many-to-one relationship between post and categories, so that a post can have one category to choose from a list of many categories, but a single category can have many posts.
Refer to figure 1.8 for creating the foreign key named “fk_post_4_category’.
That’s it for the database which I want to keep simple for the time being.
In Part II, I’ll show you how to create the Seam project and the database connection settings and in Part III the creation of the model layer by reverse engineering this very database, so it is important that you carefully follow the instructions here for creating the seamblog database.
You can download the database dump file in .sql format in the downloads section (which has some sample data in it – posts and categories entered with the finished version of the blog)