Create a Blog with JBoss Seam, Hibernate JPA and JSF then Deploy it to Tomcat in the Cloud – Part IV

Seam Tomcat Deployment Tutorial Part 4
In this part of the tutorial we will correct the bugs inherent in version 2.2 of Seam-gen and we will add some code to spice up our blog CMS.

First of all, our PostEdit.seam page is not working at all as it is. We shall fix this:

Fixing the PostEdit.xhtml page

Figure 4.1 - Fixing the PostEdit.xhtml page

Open PostEdit.xhtml from the WebContent folder and find the <rich:tabPanel switchType=”ajax”> block. This is what’s causing the problem, because it has more than one tab – one to have the user select a category from a grid on another page, and the second one for selecting a user, since these two entites have @ManyToOne relationships to the post entity, and Seam-gen can’t handle generating more than one joined object selection user interface item in this version.

I don’t want an external grid for selection of a category anyway, so we will delete the entire <rich:tabPanel> block on this page, and instead make the category selectable from a pop-up menu using a typical JSF selectOneMenu UI component.

After you delete the <rich:tabPanel> go towards the beginning of the page and locate the code block starting with <s:decorate id=”titleField” template=”layout/edit.xhtml”>.
Within that block replace <h:inputTextarea id=”title” cols=”80″ rows=”2″ required=”true” value=”#{postHome.instance.title}”/> with <h:inputText id=”title” required=”true” value=”#{postHome.instance.title}”/>

Next, add the following code block below the closing tag (</s:decorate>) for titleField:

           <s:decorate id="categoryField" template="layout/edit.xhtml">
                <ui:define name="label">Post Category</ui:define>
                <h:selectOneMenu id="category"
                           required="true"
                              value="#{postHome.instance.title}">
                     <s:selectItems value="#{categoryList.resultList}" var="cat" label="#{cat.name}" noSelectionLabel="Please select..." />
                     <s:convertEntity />
                </h:selectOneMenu>
            </s:decorate>

Continue reading

Create a Blog with JBoss Seam, Hibernate JPA and JSF then Deploy it to Tomcat in the Cloud – Part III

Seam Tomcat Deployment

In this part III of the tutorial, we’ll build the entity classes from the database we created in Part I and configure the Seam project we created in Part II so as to make it fully compatible with Apache Tomcat.

Creating The Model Layer

We’ll use Hibernate‘s reverse engineering tool (hbm2java) embedded in Seam-gen to create the entity classes from the database we created in part 1.

Seam Generate Entities

Figure 3.1 - Generating entities from database in JBoss Seam

Right-click the project blog in project explorer and select New > Seam Generate Entities.

Continue reading

Create a Blog with JBoss Seam, Hibernate JPA and JSF then Deploy it to Tomcat in the Cloud – Part I

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.

JBoss Seam Project on Tomcat

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 administartion tool:

Continue reading

Hibernate Derived Properties – @Formula Annotation

HibernateIn Hibernate a derived property (also called a calculated property) is a read-only property whose value is calculated at fetch time using SQL expressions.

Example: For an employee entity with properties such as an id and the employee name also a monthlySalary property, you might also want to have a yearlySalary which is not necessarily stored in the database.

package net.ozar.exp.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="EMPLOYEE")
public class Employee implements java.io.Serializable {

	private static final long serialVersionUID = -7311873726885796936L;

	@Id
	@Column(name="ID")
	private Integer id;

	@Column(name="FIRST_NAME", length=31)
	private String firstName;

	@Column(name="LAST_NAME", length=31)
	private String lastName;

	@Column(name="MONTHLY_SALARY")
	private float monthlySalary;

	public Employee() {
	}

	// getters and setters
     // ...

	public float getMonthlySalary() {
		return monthlySalary;
	}

	public void setMonthlySalary(float monthlySalary) {
		this.monthlySalary = monthlySalary;
	}

     /* This artificial property - as I call it - is a kind of a calculated property, but not with Hibernate derived property support - not just yet */
	public float getYearlySalary() {
		return this.monthlySalary * 12;
	}

}

The above example gives us a simple calculation in memory for just screen output without Hibernate’s derived property or the @Formula support.  Now take a moment to reflect that we need all the employees whose yearly salary average is above $5000. Then what? In this case, you might wanna make use of Hibernate’s derived property feature.
Continue reading