Full Web Application with Tomcat JSF Primefaces JPA Hibernate


Posted on June 21, 2012 by

Share it now!

Primefaces AutoComplete, JSF Converter

The JSF has the Converter tool that helps us get some data from the user view and transform into an object loaded from the database or a cache.

In the “com.converter” package create the following class:

package com.converter;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.*;

import com.facade.DogFacade;
import com.model.Dog;

@FacesConverter(forClass = com.model.Dog.class)
public class DogConverter implements Converter {

	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		DogFacade dogFacade = new DogFacade();
		int dogId;

		try {
			dogId = Integer.parseInt(arg2);
		} catch (NumberFormatException exception) {
			throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Type the name of a Dog and select it (or use the dropdow)", "Type the name of a Dog and select it (or use the dropdow)"));

		return dogFacade.findDog(dogId);

	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

		if (arg2 == null) {
			return "";
		Dog dog = (Dog) arg2;
		return String.valueOf(dog.getId());

About the above code:

  • In the @Converter annotation there is an attribute named “forClass”. This attribute indicates to the de JSF that all classes of the specified in the “forClass” will invoke the Converter. The DogConverter is annotated with “forClass = com.model.Dog.class”, every time the JSF needs a Converter for a Dog class the JSF will invoke the DogConverter. It was not necessary to write any code in the “web.xml” file.

The Converter code is required in the Primefaces AutoComplete. Bellow you see how easy is to use the AutoComplete:



About the above code:

  • The AutoComplete function has several options like minimum query length, delay to start a query, dropdown to display all values and more. It is worth to see all the options: http://primefaces.org/showcase/ui/autoCompleteBasic.jsf and http://primefaces.org/showcase/ui/autocompleteHome.jsf
  • The “complete” method has a cache of the values found in the database. The method goes to each object of the List<Dog> and keeps the matches.
  • Notice that the Converter will always be called because the “itemValue=”#{dog}”” that you will find in the AutoComplete component.

You can see bellow the AutoComplete working:

Response to Full Web Application with Tomcat JSF Primefaces JPA Hibernate

1 3 4 5
  1. Joel Mestres

    hello again, in your facade you make a new instance of DAO, besides you begin transaction on facade… both things are not good choice, aren’t them?
    I got understood that you have to manage transaction on DAO’s … and facade is just a conector between to layers… what do you think? thanks for yor answer!

    • uaihebert Post author

      Hello Joel.

      If you want it, you could create the transaction in the Facade as well, but you will have to sync in the DAO with the current transaction.

      I just used it in the DAO because this is a simple project, but if a transaction must go on between DAOS it would be better let the Facade handle the transaction.

      Thanks for passing by.

  2. Joel Mestres

    hi thansk for great full example. How can I change db provider for this? I try changing parameters in persistence.xml but result is “unable to find default provider” thanks for your help!

    • uaihebert Post author

      Hello Joel, how are you?

      In the pages of this posts it is displayed where you should change to have a different db provider for the project.

      Thanks for passing by.

  3. Hanan Ahmed

    Thanks for great tutorial, i had a small question here is the only purpose for hibernate in your tutorial is to generate the schema in other words as you are already use JPA 2 can i do without hibernate and use EclipseLink to generate my schema?

  4. Chris Georgoulis

    Hello Hebert,

    Very nice and detailed tutorial. It gave me a lot of nice ideas to improve my own web apps.
    I just have one comment:

    The Persons-Dogs are a @ManyToMany relationships. I see that when removing either dogs or persons you do not remove the other side of the relationship.

    For example a Person has many dogs. Shouldnt before you remove the person, search all the dogs(dogList) and remove the person from them(so basically updating them) – then finally remove the person himself? Shouldnt this be done in order for the bi-directional relationship to be synchronized?

    Thank you in advance,

    • uaihebert Post author

      Hello Chris, good afternoon.

      Indeed your should remove the reference of the entities. I found some other problems in the codes above, like, I forgot to add the mappedBy in the relationship also… -_-”

      Thanks for the feedback. Right now I cannot edit it, but I will do it later.

      I am sorry for the late response, I am very busy these days.

1 3 4 5

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *