Chapter 4 Generating Hibernate Persistent Objects and JSF Pages


Example 1

Here is a typical example of DAO in auto-commit, auto-close session modes:

// Getting the customer dao
CustomerDao dao =
    DaoFactory.getDaoFactory().getCustomerDao();

// Loading a customer by its primary key
Customer customer = (Customer)dao.load(new
    CustomerPK(10));

// Finding customers using an SQL statement
String sql = "select * from customer  where 
    Customer_id=2";
List customerlist = dao.findBySQLQuery(sql, null, 
    Customer.class);

// Creating an object
customer = new Customer();
customer.setId(11);
customer.setName("Guest");
customer.setEmail("guest@company.com");

// Inserting an object into the database
Seriable id = dao.save(customer);

// Modifying an object
customer.setName("VIP");
customer.setEmail("vip@company.com");

// Updating an object in the database 
dao.update(customer);

// Deleting an object from the database 
dao.delete(customer);

 


Copyright (C) 2005. Sybase Inc. All rights reserved.