Using Hibernate in a Web Application 5

(With NetBeans & MySQL)

 

 

 

 

Adding Additional Helper Methods

 

You will now add additional helper methods that create queries based on an input variable. You can check the queries in the HQL query editor.

 

  1. Add the following method to retrieve a list of categories according to filmId.

 

public Category getCategoryByID(int filmId){

    List<Category> categoryList = null;

    try {

        org.hibernate.Transaction tx = session.beginTransaction();

        Query q = session.createQuery("from Category as category where category.categoryId in (select filmCat.category.categoryId from FilmCategory as filmCat where filmCat.film.filmId='" + filmId + "')");

        categoryList = (List<Category>) q.list();

 

    } catch (Exception e) {

        e.printStackTrace();

    }

 

    return categoryList.get(0);

}

 

 

Adding additional helper methods that create queries based on an input variable

 

 

 

 

  1. Add the following method to retrieve a single film according to filmId.

 

public Film getFilmByID(int filmId){

 

    Film film = null;

 

    try {

        org.hibernate.Transaction tx = session.beginTransaction();

        Query q = session.createQuery("from Film as film where film.filmId=" + filmId);

        film = (Film) q.uniqueResult();

    } catch (Exception e) {

        e.printStackTrace();

    }

 

    return film;

}

 

Adding additional helper methods that create queries based on an input variable

 

  1. Add the following method to retrieve the film language according to langId.

 

public String getLangByID(int langId){

 

    Language language = null;

 

    try {

        org.hibernate.Transaction tx = session.beginTransaction();

        Query q = session.createQuery("from Language as lang where lang.languageId=" + langId);

        language = (Language) q.uniqueResult();

    } catch (Exception e) {

        e.printStackTrace();

    }

 

    return language.getName();

}

 

Adding method to retrieve the film language according to langId

 

Creating the JSF Managed Bean

 

In this exercise you will create a JSF managed bean. The methods in the managed bean are used for displaying data in the JSF pages and for accessing methods in the helper class to retrieve records. The JSF 2.0 specification enables you to use annotations in a bean class to identify the class as a JSF managed bean, to specify the scope and to specify a name for the bean.

To create the managed bean, perform the following steps.

 

  1. Right-click the dvdrental source package node and choose New > Other.

  2. Select JSF Managed Bean from the JavaServer Faces category. Click Next.

 

Adding new JSF managed bean to the Java web application

 

  1. Type FilmController for the Class Name.

You will use the Managed Bean name filmController as the value for the inputText and commandButton in the JSF page index.xhtml when calling methods in the bean.

  1. Select dvdrental for the Package.

  2. Type filmController for the Name that will be used for the managed bean.

  3. Set Scope to Session. Click Finish.

 

Doing some settings for new JSF managed bean

 

When you click Finish, the IDE creates the bean class and opens the class in the editor. The IDE added the @ManagedBean and @SessionScoped annotations and the name of the bean.

 

@ManagedBean(name="filmController")

@SessionScoped

public class FilmController {

 

    /** Creates a new instance of FilmController */

    public FilmController() {

    }

 

}

 

  1. Add the following fields (in bold) to the class.

 

@ManagedBean(name="filmController")

@SessionScoped

public class FilmController {

    int startId;

    int endId;

    DataModel filmTitles;

    FilmHelper helper;

    private int recordCount = 1000;

    private int pageSize = 10;

 

    private Film current;

    private int selectedItemIndex;

}

 

The IDE added the @ManagedBean and @SessionScoped annotations and the name of the bean

 

Fix your imports (Ctrl-Shift-I) and save your changes.

 

 

 

 

 

---------------------------------------------------------------------

 

Fixing more import errors

 

 

  1. Add the following code (in bold) to create the FilmController instance and retrieve the films.

 

/** Creates a new instance of FilmController */

public FilmController() {

    helper = new FilmHelper();

    startId = 1;

    endId = 10;

}

 

public FilmController(int startId, int endId) {

    helper = new FilmHelper();

    this.startId = startId;

    this.endId = endId;

}

 

public Film getSelected() {

    if (current == null) {

        current = new Film();

        selectedItemIndex = -1;

    }

    return current;

}

 

public DataModel getFilmTitles() {

    if (filmTitles == null) {

        filmTitles = new ListDataModel(helper.getFilmTitles(startId, endId));

    }

    return filmTitles;

}

 

void recreateModel() {

    filmTitles = null;

}

Adding code to create the FilmController instance and retrieve the films

 

Fix your imports (Ctrl-Shift-I) and save your changes.

 

Again, fixing unresolved imports

 

  1. Add the following methods that are used to display the table and navigate the pages.

 

public boolean isHasNextPage() {

if (endId + pageSize <= recordCount) {

return true;

}

return false;

}

 

public boolean isHasPreviousPage() {

if (startId-pageSize > 0) {

return true;

}

return false;

}

 

public String next() {

startId = endId+1;

endId = endId + pageSize;

recreateModel();

return "index";

}

 

public String previous() {

startId = startId - pageSize;

endId = endId - pageSize;

recreateModel();

return "index";

}

 

public int getPageSize() {

return pageSize;

}

 

public String prepareView(){

current = (Film) getFilmTitles().getRowData();

return "browse";

}

public String prepareList(){

recreateModel();

return "index";

}

 

 

 

 

Adding the methods that are used to display the table and navigate the pages

 

The methods that return "index" or "browse" will prompt the JSF navigation handler to try to open a page named index.xhtml or browse.xhtml. The JSF 2.0 specification enables the use of implicit navigation rules in applications that use Facelets technology. In this application, no navigation rules are configured in faces-config.xml. Instead, the navigation handler will try to locate a suitable page in the application.

 

  1. Add the following methods that access the helper class to retrieve additional film details.

 

    public String getLanguage() {

        int langID = current.getLanguageByLanguageId().getLanguageId().intValue();

        String language = helper.getLangByID(langID);

        return language;

    }

 

    public String getActors() {

        List actors = helper.getActorsByID(current.getFilmId());

        StringBuffer totalCast = new StringBuffer();

        for (int i = 0; i < actors.size(); i++) {

            Actor actor = (Actor) actors.get(i);

            totalCast.append(actor.getFirstName());

            totalCast.append(" ");

            totalCast.append(actor.getLastName());

            totalCast.append("  ");

        }

        return totalCast.toString();

    }

 

    public String getCategory() {

        Category category = helper.getCategoryByID(current.getFilmId());

        return category.getName();

    }

 

Adding methods that access the helper class to retrieve additional film details

 

Fix your imports (Ctrl-Shift-I) and save your changes.

 

Again, fixing more unresolved imports

 

Fixing the java.util.List import

 

 

 

 

 


Java, ORM & Hibernate 4 | Back to Main | Java, ORM & Hibernate 6