Creating a CRUD with JSF and Ajax using NetBeans 6.5 Beta

Pre-requisite: 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.

On 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; automaticaly generate jsf pages from an Entity Class.

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

  1. Click with right button in Java DB on Start Server (Picture 1)
  2. Click again with right button in Java DB and now click on Create Database…
  3. In the next window fill all fields according to Picture 2

Picture 1 - Starting Java DB server

Picture 2 - Creating a new database in Java DB

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

  1. Create a new web project (File->New project) in Java Web category, then choose the option Web Application (Picture 3)
  2. in the next window fill all the fields according with Picture 4
  3. Define the web server (recommended glassfish v2 or v3) and the context path (Picture 5)
  4. Click next and select the framework: JavaServer Faces (Picture 6)

Picture 3 - Creating a new web project

Picture 4 - Defining project name and project local

Picture 5 - Suggestions to choose the web server and your configurations

Picture 6 - 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.
Well, the next step is create a new Persistence Unit, an Entity Class, the JPA (Java Persistence API) controllers:

  1. Click in File->New File, select the Persistence category, so choose the option Persistence Unit and click next
  2. Click at the combobox arrow and select New Data Source …
  3. 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
  4. In Provider and Database window fill all fields according with Picture 8 and then click finish
  5. Again click File->New File, in Persistence category, select Entity Class and click next
  6. In the next window fill all fields according with Picture 9 and click finish

Picture 7 - New Persistence Unit

Picture 8 - Provider and Database

Picture 9 - Creating a new Entity class

After generating the (or NetBeans shold 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.

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:

  1. Choose Livro’s (or Book’s) Entity Classe (Picture 10)
  2. Choose the packages where will be generated the files for JPA and JSF manipulation (Picture 11), 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.

Picture 10 - Select Entity Classes

Picture 11 - Select packages to JPA and JSF

Now we have all necessary code, to test our project result, build the project (type Shift+11, clean and build project) 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 (Picture 12).

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. (Pictures 13 and 14)

Picture 12 - Project Undeploy and Deploy

Picture 13 - CRUD finished, inserting a new registry

Picture 14 - CRUD finished, registries overview after insert confirmation

Conclusion: 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.

Comments (6)

Pingnovembro 20th, 2008 at 14:54

Good tutorial — even though I use Visual JSF.

Two questions:

1. What is the differences between using “Persistence Unit” and “Entity Classes from Database”? Which one is better if we use MySQL database?

2. Did you use Ajax for this tutorial?



Catherine Ortegajaneiro 10th, 2009 at 0:02

good luck

Elenajaneiro 10th, 2009 at 17:28

Thank you!

Victorio Bormolinijaneiro 3rd, 2011 at 16:45

Parabéns pelo excelente tema e artigo. Tenho um problema de exceptio config.
Exception [EclipseLink-4003] (Eclipse Persistence Services – 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.DatabaseException Exception Description: Configuration error. Class [org.apache.derby.jdbc.ClientDriver] not found.

verifiquei que na pasta DRIVER/Serviços e me parece que o driver existe! pra se ter certeza, fiz o download, mas ainda nao funcionou.
Um detalhes, estou utilizando Tomcat. Algum problema.
desde já agradeço, um abraço Vic

Paulo Canedojaneiro 3rd, 2011 at 20:51

Olá Victorio, você precisa adicionar a biblioteca do derby (javadb) em propriedades do projeto, ou baixar manualmente os arquivos .jar e colocá-los dentro do diretório de extensões do tomcat

Attractive section of content. I just stumbled upon your website
and in accession capital to assert that I acquire
in fact enjoyed account your blog posts. Anyway I will be subscribing to your
feeds and even I achievement you access consistently quickly.

Leave a comment

Your comment