Chapter 5. Sample JSF-EJB3 Application
We use a simple "TODO" application to show how JSF and EJB3 work together in a web application. The "TODO" application works like this: You can create a new 'todo' task item using the "Create" web form. Each 'todo' item has a 'title' and a 'description'. When you submit the form, the application saves your task to a relational database. Using the application, you can view all 'todo' items, edit/delete an existing 'todo' item and update the task in the database.
Let's take a look at the contents of the Data Model represented by the Todo
class in the Todo.java
file. Each instance of the Todo
class corresponds to a row in the relational database table. The 'Todo' class has three properties: id, title and description. Each of these correspond to a column in the database table.
The 'Entity class' to 'Database Table' mapping information is specified using EJB3 Annotations in the 'Todo' class. This eliminates the need for XML configuration and makes it a lot clearer. The @Entity
annotation defines the Todo
class as an Entity Bean. The @Id
and @GeneratedValue
annotations on the id
property indicate that the id
column is the primary key and that the server automatically generates its value for each Todo
object saved into the database.
@Entity
public class Todo implements Serializable {
private long id;
private String title;
private String description;
public Todo () {
title ="";
description ="";
}
@Id @GeneratedValue
public long getId() { return id;}
public void setId(long id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) {this.title = title;}
public String getDescription() { return description; }
public void setDescription(String description) {
this.description = description;
}
}