首页 > 学院 > 开发设计 > 正文

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

2019-11-18 12:57:57
字体:
来源:转载
供稿:网友

    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));



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表