Simple CRUD Web Application with JSF 2.1, PrimeFaces 3.5, Maven and JPA 40

This is an old article, no longer supported!

 

Simple CRUD Web Application with JSF 2.1, PrimeFaces 3.5, EJB 3.1, JPA (ORM) / EclipseLink, JAAS , MySQL

In this tutorial, I will demonstrate the basics of building a simple CRUD Web Application with JSF 2.1, PrimeFaces 3.5, EJB 3.1, JPA 2 (ORM) / EclipseLink, JAAS, Maven 2, MySQL 5.

To be able to run the sample project (download the source code here) you need to install the required software:
Please try live demo (Username: Admin, Password:1234)

    NetBeans 7.2 IDE
    GlassFish V3 application server
    MySQL server
    Java EE 6 (JDK 1.6),
    Maven 2

(There is a good instruction on how to install Netbeans 7.2 http://netbeans.org/community/releases/72/install.html)
I will briefly outline the project here but later on I will write more details about the project under each sections and post them once they are available. If you have any suggestions for improving this article I would love to hear from you.

Tutorial parts
1- Creating ORM entity classes with Plain Old Java Objects and SQL tables for the project
2- Securing your web application with Glassfish security realms in JAAS
3- Writing the Enterprise Java Beans, Create, Read, Update and Delete (CRUD) operations with JPA 2 EclipseLink (ORM)
4- Lazy Loading / Pagination with JSF 2.1 / Primefaces 3.5 DataTable and Form Validations
5- Downloading PrimeFaces themes / creating and installing custom themes for the project and overriding PrimeFaces style sheets and using PrimeFaces layouts.

6- Generic PrimeFaces LazySorter

Update:

I have updated faces-config.xml file to fix a bug caused  (after a validation error) due to the incorrect state of the input fields.  Thanks to John for finding this error and kindly letting me know about it.

To fix problem we need to add this dependency to our project’s pom.xml file

<dependency>
    <groupId>org.omnifaces</groupId>
    <artifactId>omnifaces</artifactId>
    <version>1.4.1</version>
</dependency>

and then add this code line to faces-config.xml file to register ResetInputAjaxActionListener as a phase-listener

<lifecycle>
    <phase-listener>org.omnifaces.eventlistener.ResetInputAjaxActionListener</phase-listener>
</lifecycle>

(You can download this file! Source code is up to date.)

For more information please see Omniface’s website

Some screenshots from Simple CRUD web application;

Login page:

User listing page:

User creating and editing dialogue:

JSF 2.1, PrimeFaces 3.5, EJB 3.1, JPA (ORM) / EclipseLink, JAAS , MySQL, CRUD

40 thoughts on “Simple CRUD Web Application with JSF 2.1, PrimeFaces 3.5, Maven and JPA

  1. Reply ahmed Dec 28,2012 11:02 pm

    Hi,I’am student and I’d like create a crud with a primefaces & jpa2 ,please send me this example :)

  2. Reply Dagie Mar 15,2013 7:39 pm

    Do you have eclipse version on this tutorials?

    • Reply simtay Mar 15,2013 7:44 pm

      No Eclipse version at the moment. I guess you can convert it to Eclipse version easily! All you need is an embedded GlassFish server on you Eclipse! Cheers Emre

  3. Reply john Mar 24,2013 2:17 am

    First of all many thanks for the nice article.
    I should also notice that there is bug caused due to the inappropriate state of the partially render input fields. You can reproduce the bug following the steps described below:
    1. Click on a user link form the list to show the “User Detail” dialog
    2. Modify the users details with invalid values
    3. Click on “Update” button
    4. After the validation failure, click on a user link again (select a different user now).
    Besides the input fields that failed at validation, all other fields are not updated.
    This is happening because the “input” fields of the “User Detail” dialog do not participate at the ‘execute’ part of the AJAX request performed by the link at the user’s list table. The local values of the “User Detail” dialog’s components are still set due to the previous validation failure (when we have tried to update the user). When JSF renders input components, it tests, in this order, if the submitted value, the local value and finally the model value is not null. The first not null value is found, is the value that displays.

    You could fix the problem by applying an overall solution provided by the omnifaces (http://code.google.com/p/omnifaces/), registring the org.omnifaces.eventlistener.ResetInputAjaxActionListener as phase listener. The primefaces seems to offer a less generic solultion by using the resetInput tag (http://www.primefaces.org/showcase/ui/resetInput.jsf).

    • Reply simtay Mar 25,2013 11:37 am

      Hey John,

      Thank you so much for pointing that out, you are awesome! I have fixed the issue by following your suggestion and updated the code on git-hub. I have reproduced the bug like you said it wasn’t resetting the form fields when submission has a validation error.
      I simply updated the ‘faces-config.xml’ file. I have registered org.omnifaces.eventlistener.ResetInputAjaxActionListener as a phase listener (it is explained here http://showcase.omnifaces.org/eventlisteners/ResetInputAjaxActionListener)

      Thank you again

  4. Reply Ashish Mar 24,2013 5:58 am

    Hey friend. Can u tell me how did u managed to install glassfish server in the rhcoud. Openshift.

  5. Reply Nitin Apr 6,2013 9:13 pm

    Hi, Simtay I have successfully deployed this app in netbeans but I am getting an error on browser, please help

    ERROR:
    HTTP Status 500 -
    type Exception report

    message

    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Component ID v2jebr3-retrieve-update has already been found in the view.
    root cause

    java.lang.IllegalStateException: Component ID v2jebr3-retrieve-update has already been found in the view.
    note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.

    • Reply simtay Apr 8,2013 7:41 am

      That’s a strange error never had this one before. Did you clear your project completely and build it with dependency? Maybe you should ask it on Stackoverflow. Anyway, let me know if there is anything else I can do. Cheers

      • Reply Nitin Apr 8,2013 4:59 pm

        I don’t know what is happening but now I am getting this error.
        I need your help

        COMPILATION ERROR :
        ————————————————————-
        Failure executing javac, but could not parse the error:
        javac: -endorseddirs requires an argument
        Usage: javac
        use -help for a list of possible options

  6. Reply Caterpillar Apr 7,2013 8:34 pm

    Hi, I need to know if this project can be runned on Tomcat 7.0 since I am developing a JSF+JPA project for Tomcat and I found out a lot of usefull tips in your example

    • Reply simtay Apr 8,2013 7:39 am

      You should be able to, never tried it. I guess you will have to do some changes on the authorization and authentication part. Cheers…

  7. Reply nisma Apr 26,2013 7:19 am

    i can use primefaces 3.2 in this application ?

  8. Reply Keith May 2,2013 2:08 pm

    hi,

    I have java7 and netbeans 7.3

    I downloaded the source and get erros in the UserList.xhtml and Login.xhtml
    Is this a version issue?

    thanks,
    Keith

    • Reply admin May 2,2013 2:12 pm

      Hey Keith,

      Yes I have tried it with 7.3 just now and yes I guess it is a version issue but still working. I will try to fix that issue and update the code! Thanks for pointing it out…

  9. Reply Rainbow May 12,2013 6:31 am

    Hi Dear Simtay ,
    I saw your template crud and it’s interesting ,
    would you put the your updated version of this code on your website ?
    And one more thing , would you please make it somehow to create database itself with some change inside the persistance xml file ,
    I want to use SQL Server instead of my sql and need to use also hibernate in it ,
    I’ve downloaded your code but still couldn’t run it .
    All the best .
    Rainbow Man .

    • Reply admin May 12,2013 11:01 am

      Hey, I have updated the pages. You only need to create user role view and do the initial population by hand. Please see part-2.

  10. Reply Peter May 13,2013 10:02 am

    Great job, thanks for posting. I noticed that you have two different dialogs, one for creating a new user and another for editing an existing user. Both dialogs have the same input fields, with the exception of user id. Isn’t is possible to have a single dialog? I’d do it myself, however my knowledge of primefaces is limited.
    thanks
    Peter

    • Reply simtay May 21,2013 11:49 am

      You are welcome, yes it’s better if there was only one dialog. I will change it when I have the time.

  11. Reply anazri Aug 6,2013 4:07 am

    Hi Simtay

    I want to request your suggestion of codes changes if i develop in Eclipse with MySQL connections. I wish to add the hibernate too. Thanks

    • Reply simtay Aug 11,2013 7:07 pm

      You don’t need to change anything all you need to do is to integrate Glassfish server to your Eclipse IDE. Cheers Emre…

  12. Reply Caterpillar Aug 14,2013 2:56 am

    Did you use any JTA layer in the project?

  13. Reply Caterpillar Aug 23,2013 10:33 am

    I am looking for a mock framework to test the correct JPA/JTA configuration of the container. Have you ever used one of them in the development of this example?

  14. Reply tolga Sep 11,2013 2:21 am

    i could’nt catch the point that how this connect to the database? there is no database pass, name anywhere except create sql.

    • Reply simtay Sep 11,2013 7:22 am

      Hi Tolga, please see Part 2 of this tutorial. There I explained how to create a database pool and connecting to it using persistence.xml file. Cheers

  15. Reply dukgo Oct 1,2013 7:10 am

    hey man this is really good. i’m looking for a tutorial and sample webapp on jsf because i want to learn more. thanks very much for this.

    question, can this be converted to eclipse project? and can it run on tomcat?

    thanks!

  16. Reply Ferdie Oct 11,2013 2:55 pm

    Excellent…I believe for many java’s beginner they will call you Java Guru. Guru is always share their knowledge.

  17. Reply Kay Wrobel Oct 29,2013 8:38 am

    Hi Simtay.

    I just wanted to take the opportunity to thank you for the inspiration you gave me. I have used your ideas and some code snippets and turned them into a CRUD Generator for NetBeans. The project has received very healthy feedback and the NetBeans itself is now implementing a similar design.

    I feel like this thank you was long overdue since I started the project back in Feb. 2013. So,

    THANK YOU!

    • Reply simtay Oct 31,2013 7:39 pm

      Hi Kay,

      Thanks so much for your kind words, I am very happy this application gave you the inspiration to create the CRUD generator for NetBeans and thanks for letting me know and giving me the motivation for my next project.

      Cheers

  18. Reply Taysay Oct 31,2013 2:02 am

    Thanks so very much, for this great peace. Please will it work on Jboss/ tomcat ?
    Many thanks , have a wonderful day

    • Reply simtay Oct 31,2013 7:35 pm

      Hey Taysay,
      This application would work on a Java EE application server but you will have to re implement the security part and no it won’t work on Tomcat not a Java EE server.
      You have a good day too.

  19. Reply Caterpillar Nov 20,2013 6:12 am

    Will removing JAAS part, require much work? I am thinking about removing it if it will take too much time to implement it into Tomcat environment

Leave a Reply

  

  

  


seven × = fourteen