Chapter 4 Generating Hibernate Persistent Objects and JSF Pages
The following example shows how to use DAO in non-auto-commit and non-auto-close session modes:
// Getting the customer dao
CustomerDao dao =
DaoFactory.getDaoFactory().getCustomerDao();
// Change the default mode
dao.setAutoCloseSession(false);
dao.setAutoCommit(false);
// Begin transaction
dao.beginTransaction();
// 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);
// Commit transaction
dao.commitTransaction();
// Closing the connection
dao.closeSession();
| Copyright (C) 2005. Sybase Inc. All rights reserved. |
| |