Hibernate3.3(2)

    xiaoxiao2021-12-14  19

      hibernate的核心类和接口

    ①      Configuration 类

    它的用处是:

    1.      读取hibernate.cfg.xml

    2.      管理对象关系映射文件 <mapping resource=””>

    3.      加载hibernate 的驱动,url ,用户..

    4.      管理hibernate配置信息

     

    ②      hibernate.cfg.xml

    ③      对象关系映射文件

    ④      SessionFactory (会话工厂)

    1.      可以缓存sql语句和数据(称为session级缓存)!!

    2.      是一个重量级的类,因此我们需要保证一个数据库,有一个SessionFactroy

    这里我们讨论一个通过SessionFactory获取 Session的两个方法 openSession() 一个 getCurrentSession();

    1.      openSession() 是获取一个新的session

    2.      getCurrentSession () 获取和当前线程绑定的session,换言之,在同一个线程中,我们获取的session是同一session,这样可以利于事务控制

    如果希望使用getCurrentSession 需要配置 hibernate.cfg.xml中配置.

    3.      如何选择

    原则:

           ①如果需要在同一线程中,保证使用同一个Session则,使用getCurrentSession()

           ②如果在一个线程中,需要使用不同的Session,则使用opentSession()

    4.      通过 getCurrentSession() 获取的session在事务提交后,会自动关闭,通过openSession()获取的session则必须手动关闭

    5.      如果是通过getCurrentSession() 获取 sesssion ,进行查询需要事务提交.

     

    全局事务和本地事务

    jndi

     

    l  如何确定你的session有没有及时关闭

    windows  cmd netstat –an      [oracle 1521mysql 3306 sql server 1433]

    linux/unix  netstat –anp top 

     

    ⑤      session接口

    它的主要功能和作用是:

    1.        Session一个实例代表与数据库的一次操作(当然一次操作可以是crud组合)

    2.        Session实例通过SessionFactory获取,用完需要关闭。

    3.        Session是线程不同步的(不安全),因此要保证在同一线程中使用,可以用getCurrentSessiong()。

    4.        Session可以看做是持久化管理器,它是与持久化操作相关的接口

     

    u  get vs load

    1.       如果查询不到数据,get 会返回 null,但是不会报错, load 如果查询不到数据,则报错ObjectNotFoundException

    2.      使用get 去查询数据,(先到一级/二级)会立即向db发出查询请求(select ...), 如果你使用的是 load查询数据,(先到一级、二级))即使查询到对象,返回的是一个代理对象,如果后面没有使用查询结果,它不会真的向数据库发select ,当程序员使用查询结果的时候才真的发出select ,这个现象我们称为懒加载(lazy)

    3.      通过修改配置文件,我们可以取消懒加载

    <class  name="Employee"lazy="false" table="employee">

    4.      如何选择使用哪个: 如果你确定DB中有这个对象就用load(),不确定就用get()(这样效率高)

     

    u  我们对获取session的工具类,升级,让它可以直接返回 全新的session和线程相关的session

    代码:

    packagecom.hsp.util;

    importorg.hibernate.Session;

    importorg.hibernate.SessionFactory;

    importorg.hibernate.cfg.Configuration;

    final publicclass HibernateUtil { //SqlHelper

         private static SessionFactorysessionFactory=null;

         //使用线程局部模式

         private static ThreadLocal<Session>threadLocal=new ThreadLocal<Session>();

         private HibernateUtil(){};

         static {

                sessionFactory=newConfiguration().configure("com/hsp/config/hsp.cfg.xml").buildSessionFactory();

         }

        

         //获取全新的全新的sesession

         public static Session openSession(){

                return sessionFactory.openSession();

         }

         //获取和线程关联的session

         public static Session getCurrentSession(){

               

                Session session=threadLocal.get();

                //判断是否得到

                if(session==null){

                       session=sessionFactory.openSession();

                       //把session对象设置到 threadLocal,相当于该session已经和线程绑定

                       threadLocal.set(session);

                }

                return session;

               

               

         }

        

        

    }

     

    u  query接口

    通过query接口我们可以完成更加复杂的查询任务.

    举例: 通过用户来查询数据.

    快如入门:

    Sessionsession=HibernateUtil.getCurrentSession();

    Transaction ts=null;

    try {

    ts=session.beginTransaction();

    //获取query引用[这里 Employee不是表.而是domain类名]

    //[where 后面的条件可以是类的属性名,也可以是表的字段,安照hibernate规定,我们还是应该使用类的属性名.]

    Query query=session.createQuery("fromEmployee where namehsp='shunping'");

    //通过list方法获取结果,这个list会自动的将封装成对应的domain对象

    //所以我们jdbc进行二次封装的工作没有.

    List<Employee> list=query.list();

    for(Employee e: list){

    System.out.println(e.getAaaid()+""+e.getHiredate());

    }

    ts.commit();

    } catch (Exception e) {

    if(ts!=null){

    ts.rollback();

    }

    throw new RuntimeException(e.getMessage());

    }finally{

    //关闭session

    if(session!=null&&session.isOpen()){

    session.close();

    }

    }

    u  criteria 接口的简单使用

    快如入门:

    Sessionsession=HibernateUtil.getCurrentSession();

    Transaction ts=null;

    try {

    ts=session.beginTransaction();

    Criteriacri=session.createCriteria(Employee.class).

    setMaxResults(2).addOrder(Order.desc("id"));

    List<Employee> list=cri.list();

    for(Employee e: list){

    System.out.println(e.getAaaid());

    }

    ts.commit();

    } catch (Exception e) {

    if(ts!=null){

    ts.rollback();

    }

    throw new RuntimeException(e.getMessage());

    }finally{

    //关闭session

    if(session!=null&&session.isOpen()){

    session.close();

    }

    }

     

    u  如何使用eclipse进行hibernate 快速开发

    我们以前面讲的对employee表进行crud为例,演示具体用法

     

    手动配置:

    db(table )->手写domain对象->对象关系映射文件

    现在我们希望用工具完成 Domain对象和 关系映射文件的工作.

     

    1.      创建web项目

    2.      通过myeclipse 提供 数据库浏览器连接到我们的oracle数据库(多人开发)

    * 这里请大家小心,如果我们测试

     

    你们把自己的数据库通过 db 浏览器连接上

     

    引入hibernate开发包.,同时自动创建hibernate.cfg.xml文件,如果希望把hibernate开发包升级,我们可以重新引入包.

     

     

    下面我们使用myeclipse提供的逆向工程,自动的创建domain类和对象关系映射文件.

     

     

    java对象(属性) <---------1. java类型 2. hibernatetypes-------------> 表字段类型

     

    拉通练习一把.

    转载请注明原文地址: https://ju.6miu.com/read-969449.html

    最新回复(0)