Using Hibernate

in a Java Swing Application 7

 

 

 

  1. Adding the Query to the Form

 

Adding the Query to the Form

 

You now need to modify MyDVDStoreAdmin.java to add the query strings and create the methods to construct and invoke a query that incorporates the input variables. You also need to modify the button event handler to invoke the correct query and add a method to display the query results in the table.

Open MyDVDStoreAdmin.java and click the Source tab.

 

 

Add the following query strings (in bold) to the class.

 

public MyDVDStoreAdmin() {
    initComponents();
}
 
private static String QUERY_BASED_ON_FIRST_NAME="from Actor a where a.firstName like '";
private static String QUERY_BASED_ON_LAST_NAME="from Actor a where a.lastName like '";

 

The following is the screenshot for the code. It is possible to copy the queries from the HQL Query Editor tabs into the file and then modify the code.

 

Adding query strings to MyAdminStore class

 

Add the following methods to create the query based on the user input string. The methods call a method called executeHQLQuery() and create the query by combining the query string with the user entered search string.

 

private void runQueryBasedOnFirstName() {
    executeHQLQuery(QUERY_BASED_ON_FIRST_NAME + firstNameTextField.getText() + "%'");
}
    
private void runQueryBasedOnLastName() {
    executeHQLQuery(QUERY_BASED_ON_LAST_NAME + lastNameTextField.getText() + "%'");
}

 

The following is the screenshot. Just leave the error at this stage we will add the executeHQLQuery() method in the next step.

 

Adding runQueryBasedOnFirstName() and runQueryBasedOnlastName() methods the class

 

Add the executeHQLQuery() method. The executeHQLQuery() method calls Hibernate to execute the selected query. This method makes use of the HibernateUtil.java utility class to obtain the Hibernate Session.

 

private void executeHQLQuery(String hql) {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        displayResult(resultList);
        session.getTransaction().commit();
    } catch (HibernateException he) {
        he.printStackTrace();
    }
}

 

The following is the screenshot for the code.

 

Adding the executeHQLQuery() method to the class

 

Fix your imports to add import statements for the Hibernate libraries (org.hibernate.Query and org.hibernate.Session) and java.util.List.

 

Invoking the Fix Imports context menu from NetBeans 6.9.1 source code editor

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

Fixing the related imports

 

Just leave the last error as is. We will add the displayResult() method later.

 

The last error seen for unresolved displayResult() method

 

Create a Query button event handler by switching to the Design view and double-clicking the Query button or select the button > right-click mouse > select Events menu > select Action menu > select actionPerformed menu.

 

adding the event handler for query button through actionPerformed context menu

 

The IDE creates the queryButtonActionPerformed() method and displays the method in the Source view. Modify the queryButtonActionPerformed() method in the Source view by adding the following code so that a query is run when the user clicks the button.

 

private void queryButtonActionPerformed(java.awt.event.ActionEvent evt) {
    if(!firstNameTextField.getText().trim().equals("")) {
        runQueryBasedOnFirstName();
    } else if(!lastNameTextField.getText().trim().equals("")) {
        runQueryBasedOnLastName();
    }
}

 

The following is the screenshot for the code.

 

Adding the queryButtonActionPerformed() method to handle the event when the Query button is clicked

 

Add the following method to display the results in the JTable table.

 

private void displayResult(List resultList) {
    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("ActorId"); 
    tableHeaders.add("FirstName");
    tableHeaders.add("LastName");
    tableHeaders.add("LastUpdated");
 
    for(Object o : resultList) {
        Actor actor = (Actor)o;
        Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(actor.getActorId());
        oneRow.add(actor.getFirstName());
        oneRow.add(actor.getLastName());
        oneRow.add(actor.getLastUpdate());
        tableData.add(oneRow);
    }
    resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
}

 

The following is the screenshot for the code.

 

Adding the displayResult() method to display the query result in a table

 

Fix your imports to add java.util.Vector and save your changes.

After you save the form you can run the project.

 

Invoking the Fix Imports context menu

 

Fixing the related imports

 

 

 

 

 

 

 


< Java Swing and Hibernate 6 | Java and NetBeans Tutorial | Java Swing and Hibernate 8 >