Hibernate数据库操作优化

/*修改数据*/
import org.hibernate.cfg.Configuration;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
class 
{
 public static void main(String[] args)
 {
  /*1、读取Hibernate配置文件*/
  Configuration conf=new Configuration().configure();
  
  /*3、打开Session*/
  org.hibernate.Session session=util.HibernateSessionFactory.getSession();

  Customer cus=new Customer();
  /*5、启动事务*/
  Transaction tran=session.beginTransaction();
  session.load(cus,"222");
  cus.setCbalance(cus.getCbalance()+1000);
  Transaction tran=session.beginTransaction();
  try{
  
  /*修改数据*/
  session.update(cus);
  tran.commit();
  }
  catch(Exception ex)
  {
   tran.rollback();
  }
  finally
  {
   util.HibernateSessionFactory.closeSession();
  }

 }
}
/*查找数据*/

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
class 
{
 public static void main(String[] args)
 {
  Session session=util.HibernateSessionFactory.getSession();
  Customer cus=(Customer)session.get(Customer.class,"009");
  System.out.println(cus.getCaccount());
  System.out.println(cus.getPassword());
  System.out.println(cus.getCbalance());
  System.out.println(cus.getUsername());
  util.HibernateSessionFactory.closeSession();


 }
}

 

posted on 2009-08-09 00:04  草原和大树  阅读(770)  评论(0编辑  收藏  举报