Creating a CRUD with JSF and Ajax using NetBeans 6.5 Beta

Contributed By; Paulo Canedo

[[{TableOfContentsTitle=TableOfContents} | {TableOfContents title='Table of Contents'}]]


In this tutorial, we will see how to create a CRUD application (Create, Read, Update and Delete) using the NetBeans IDE 6.5 Beta. The main features demonstred in this tutorial are-

  • Work with a web project using glassfish and Java Server Faces,
  • Create a Persistence Unit,
  • Create an Entity Class, and
  • Automaticaly generate JSF pages from an Entity Class


NetBeans 6.5 Beta, or most recent version with the “Java Web and EE” or “All” pack. The glassfish version 2 or 3 must be installed.

Getting Started

Let’s start, first of all go to the Services Tab (Ctrl+5), open Databases node and follow the steps bellow-

  • Click with right button in Java DB on Start Server

File:Starting Java DB Server

  • Click again with right button in Java DB and now click on Create Database…
  • In the next window fill all fields.

File:Creating a new database in Java DB

Create Project

After the database creation, so let’s go to our NetBeans project;

  • Create a new web project (File->New project) in Java Web category, then choose the option Web Application

File:Creating a new web project

  • In the next window, fill all the fields.

File:Defining project name and project local

  • Define the web server (recommended glassfish v2 or v3) and the context path

File:Suggestions to choose the web server and your configurations

  • Click Next and Select the Web Framework- JavaServer Faces

File:Adding the framework Java Server Faces to your project

Now your project is created, to test if your project is working correctly with the web server, click with rigth button in Project Tab (Ctrl+1) and click Run.

Persistence Unit, Entity Classes and JPA

Well, the next step is create a new Persistence Unit, an Entity Class, the JPA (Java Persistence API) controllers:

  • Click in File > New File, select the Persistence category, so choose the option Persistence Unit and click Next.

File:New Persistence Unit

  • Click at the combobox arrow and select New Data Source …
  • Type a name to jndi (eg: dsExemploJsf or dsSampleJsf), at Database Connection select the connection with the database that you created in the begin of this tutorial, click OK.
  • In Provider and Database window fill all fields and click Finish.

File:Provider and Database

  1. Again click File->New File, in Persistence category, select Entity Class and click next
  2. In the next window fill all fields and click Finish.

File:Creating a new Entity class

Auto-Generated Source Code

After generating the (or NetBeans should automatically open it, let’s edit this file that way-

@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String isbn;
private String title;
private String author;
private Integer numberOfPages;
private Double weight;

public Long getId() {
return id;

Then put the cursor position at line imediatlly after:

public void setId(Long id) { = id;

Then press simultaneously- Alt-Insert, select Getter and Setter… then select all fields and click OK.

JSF Pages from Entity Classes

After creation of the Entity Class, let’s go to more one netbeans wizard wich will generate all necessary code to work with JPA and JSF files, so click in File->New File, in Persistence category, choose the option: JSF Pages From Entity Classes, and follow steps;

  • Choose Livro’s (or Book’s) Entity Classe (Picture 10)

File:Select Entity Classes

  • Choose the packages where will be generated the files for JPA and JSF manipulation (see following figure), here note that the option “Ajax-enable generated pages” is displayed, this option allows generated code to use Ajax technology, remember that this is only available from version 6.5 beta or the most recent version.

File:Select packages to JPA and JSF

Clean, Build and Run

Now we have all necessary code, to test our project result, build the project (type Shift+11, clean and build project).

Undeploy and Deploy

Click with right button in project name by Projects tab (Ctrl+1), after click “Undeploy and Deploy” to remove the web project of server (if there is) and deploy version that you already finished building. File:Project Undeploy and Deploy

Output Window

If there are no errors message at Output tab (Ctrl+4), you can run the project using F6 key and see your CRUD working wonderful. File:CRUD finished, inserting a new registry File:CRUD finished, registries overview after Insert confirmation


It’s not recommendable the use of this wizard to generate CRUD by NetBeans for people who don’t have experience with Java Server Faces to do their production work day by day.

The importance of this NetBeans feature for this public is that they have access to development step by step about the creation of a CRUD using these wonderful Java frameworks. The learning on this activity is quite satisfactory.

For those who already have experience just a look at the creation process and results will teach what to do. For those who want something practical, without much detail, and that is quite fast to do, this guide is quite helpful.

Revised by Ana Lyvia Liveira.

See Also

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo