WEB开发网
开发学院WEB开发Jsp 编程技术分享:Hibernate开发心得 阅读

编程技术分享:Hibernate开发心得

 2008-01-05 10:09:44 来源:WEB开发网   
核心提示: hibernate核心接口: session 、 Transaction 、 Query 、Configuration Configuration :配置、根启动Hibernate 创建SessionFactory对象 SessionFactory :初始化Hibernate创建Session对象 Sess

   hibernate核心接口:

   session 、 Transaction 、 Query 、Configuration

   Configuration :配置、根启动Hibernate 创建SessionFactory对象

   SessionFactory :初始化Hibernate创建Session对象

   Sesssion :负责保存、更新、删除、加载和查询对象

   Transaction :治理事务

   Query和Criteria : 执行数据库查询

   Query query = session.createQuery("from Customer c where c.name =:name");
   query.setParameter("name","tom",Hibernate.STRING);

   Sessin 接口方法 :

   save();
   updata();
   delete();
   load();
   find();

   Session 通过 SessionFactory.opensession()方法获得;
   控制事务一起Transaction
   Session session = SessionFactory.openSession();
   Transaction tx = null ;
   try{
     tx = session.begionTransaction();
     //逻辑方法
     tx.commit();
   }
   catch(Exception e){
      e.PRintStackTace();
      if(tx != null)tx.rollback();
      thows e ;
   }
   finlly{
      session.close();
   }

   //session.save(customers)执行下列SQL语句:

   insert into CUSTMERS (ID , NAME , EMAIL , PASSWord , PHONE , ADDRESS , SEX , IS_MARRIED , DESCRipTION ,
    IMAGE , BIRTHDAY , REGISTERED_TIME ) VALUES (1 , 'TOM' , '1234', 55556666 ,'SHANGHAI','M' , 0 ,
    'I AM VERY HONEST' @ ,'1980-05-06' , NULL);

   //session.find("from customer as c order by c.name asc")执行下列SQL语句:
   select * from CUSTOMERS order by name asc ;

   //(customer)session.load(customer.class,customer_id)执行下列SQL语句:
   select * from CUSTOMERS WHERE ID = 1 ;
   update CUSTOMERS SET NAME = 'TOM' ,EMAIL ='TOM@YAHOO.COM' ,ADDRESS = 'BEIJING' WHERE ID = 1;

   //session.delete("from CUSTOMER AS C");执行下列SQL语句:
   select * from CUSTOMERS ;
   DELETE FROM CUSTOMERS WHERE ID = 1;
   或是:session.load(customers.class,id);
      session.delete(customer);
   或是:
      session.load(customers.class,new int(1));


Tags:编程 技术 分享

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接