Jump to Menu

How to Use Hibernate Criteria

Object-Relational Mapping (ORM) provides many ways to retrieve data from database. Criteria is one of the efficient and easy way to query database. This tutorial will show you how to generate and use Criteria to retrieve data in Visual Paradigm.

Compatible edition(s): Enterprise, Professional, Standard

  • February 18, 2010
  • Views: 32,373
  • PDF

Generating Criteria

  1. Open CriteriaTest.vpp in Visual Paradigm.
  2. Select Tools > Hibernate > Generate Code... from the application toolbar.
  3. The Database Code Generation window appear. Keep the option Generate Criteria selected. Click OK to start generating ORM code.
    Generate criteria
  4. If you don't have any default database chosen, you are now asked to select one. Select the DBMS you use and fill in the connection settings in the Database Configuration window, and then confirm.
    Select MySQL in database configuration

  5. Criteria classes are generated for each ORM persistable class.

Using Criteria

  1. Execute the Java class CreateOrmDatabaseSchema in the generated ormsamples package to create database tables.
  2. Download the sample java code Create.java. You can also find this file at the bottom of this tutorial.
  3. Execute the Java class Create to create sample data.
  4. Create a class with the following code to retrieve all open order, sort by descending order data:
    OrderCriteria orderCriteria = new OrderCriteria();
    orderCriteria.status.eq(Order.STATUS_OPEN);
    orderCriteria.orderDate.order(false);
    Order[] listOrder = orderCriteria.listOrder();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    for (int i = 0; i < listOrder.length; i++) {
    	System.out.println(listOrder[i].getCustomer().getAccountNo() + ", "
    			+ dateFormat.format(listOrder[i].getOrderDate()) + ", "
    			+ (listOrder[i].getStatus() == Order.STATUS_OPEN));
    }
    

More Examples on Criteria

  • Join multiple tables with conditions:
    OrderLineCriteria orderLineCriteria = new OrderLineCriteria();
    // quantity less than 5
    orderLineCriteria.quantity.lt(5);
    // join order table
    OrderCriteria orderCriteria = orderLineCriteria.createOrderCriteria();
    orderCriteria.status.eq(Order.STATUS_OPEN);
    // join customer table
    CustomerCriteria customerCriteria = orderCriteria.createCustomerCriteria();
    customerCriteria.accountNo.eq("C1232122");
    OrderLine[] listOrderLine = orderLineCriteria.listOrderLine();
    for (int i = 0; i < listOrderLine.length; i++) {
    	System.out.println(listOrderLine[i].getItemNo() + ", " + listOrderLine[i].getQuantity());
    }
    
  • Calculate the total quantity of all open orders:
    OrderLineCriteria orderLineCriteria = new OrderLineCriteria();
    // join order table
    OrderCriteria orderCriteria = orderLineCriteria.createOrderCriteria();
    orderCriteria.status.eq(Order.STATUS_OPEN);
    orderLineCriteria.setProjection(Projections.sum(OrderLine.PROP_QUANTITY));
    System.out.println(orderLineCriteria.uniqueResult());
    
  • Controlling the first and number of results, useful for pagination:
    OrderCriteria orderCriteria = new OrderCriteria();
    orderCriteria.setFirstResult(11);
    orderCriteria.setMaxResults(10);
    Order[] listOrder = orderCriteria.listOrder();
    for (int i = 0; i < listOrder.length; i++) {
    	System.out.println(listOrder[i].getID());
    }
    



Turn every software project into a successful one.

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK