`
隐形的翅膀
  • 浏览: 484625 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate SessionFactory and Session

阅读更多
1. Hibernate 的主类是Session类,它提供查找,保存和删除映射对象的方法,必须来建立一个SessionFactory,来得到Session

   SessionFactory 有三个重要的属性, datasource,mappingLocation,hibernateProperties
   <bena id="hibernateSessionFactory">
       <property name="dataSource" ref="dataSource"/>
       <property name="mappingLocations">
            <list>
                 <value>classpath*:/com/test/*.hbm.xml</value>
            </list>
       </property>
       <property name="hibernateProperties">
                 ......
       </property>
   </bean>


2. 直接使用Hibernate SessionFactory and Session
  
   ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
   SessionFactory sessionFactory=(SessionFactory)ac.getBean("hibernateSessionFactory");
   Session session=sessionFactory.openSession();
   // do your work
   session.close();
   

3. 使用Hibernate session 应该从调用beginTransaction 开始, 到调用Transaction.commit()方法结束,如果抛出异常,我们可以回滚事务
 
   ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
   SessionFactory sessionFactory=(SessionFactory)ac.getBean("hibernateSessionFactory");
   Session  session=null;
   Transaction transaction=null;
   try{
         session=sessionFactory.openSession();
         transaction=session.beginTransaction();
         
         // do your work
         tramsactopm.commit();
   }catch(Exception e){
         if(transaction!=null)transaction.rollback();
         throw e;
   }finally{
         if(session!=null) session.close();
   }
   

4. 两种得到Spring 实现的 HibernateTemplate 的方法
  
   ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
   SessionFactory sessionFactory=(SessionFactory)ac.getBean("hibernateSessionFactory");
   HibernateTemplate template=new HibernateTemplate(sessionFactory)
   template.execute(new HibernateCallback(){
        public Object doInHibernate(Session session) throws HibernateException,SQLException{
           //do the work
           return null
        }
   })
   

  在配置文件中
 
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
        <property name="sessionFactory" ref="hibernateSessionFactory"/>
   </bean>
   HibernateTemplate template=(HibernateTemplate)at.getBean("hibernateTemplate");
   template.execute(new HibernateCallback(){
        public Object doInHibernate(Session session) throws HibernateException,SQLException{
           //do the work
           return null
        }
   })
  

5. Hibernate  不支持泛型,要对自己的代码进行单元测试和集成测试,保证返回了正确的对象

6. 如何简写DAO
  省去了在每个DAO中写 sessionFactory 的麻烦
 
  <bean id="hibernateDaoSupport" abstract="true" class="org.springframework.orm.hibernate.support.HibernateDaoSupport">
       <property name="sessionFactory" ref="hibernateSessionFactory"/>
    </bean>
    <bean id="dao1" class="com.test.class1" parent="hibernateDaoSupport"/>
    <bean id="dao2" class="com.test.class2" parent="hibernateDaoSupport"/>
    <bean id="dao3" class="com.test.class3" parent="hibernateDaoSupport"/>

  

分享到:
评论

相关推荐

    HibernateSessionFactory.java Hibernate使用的整合的工具文件

    Session s= HibernateSessionFactory.getSession(); 就是Hibernate的工具java类

    HibernateSessionFactory

    用于获得Session会话及关闭Session会话

    spring配置sessionFactory(spring3.2.3+hibernate4.2.2)

    一个实例小工程,讲解的是将hibernate的sessionFactory交给spring管理的配置方法

    新Hibernate SessionFactory().getCurrentSession()猫腻

    NULL 博文链接:https://zgdkik.iteye.com/blog/1835667

    J2EE利用Hibernate采用B/S架构网页设计

    public class HibernateSessionFactory { /** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration ...

    Hibernate Web应用的开发步骤

    (5)创建Hibernate的SessionFactory类。 (6)通过SessionFactory创建Session实例。 (7)通过创建的Session实例进行持久化对象的管理。 (8)通过创建的Transaction实例进行事务管理。 (9)通过创建的Query或...

    Hibernate学习笔记和资料

    hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...

    hibernate API 文档3.6.8.chm

    a native API comprised mainly of SessionFactory and Session an implementation of the JSR-317 Java Persistence API (JPA) specification comprised mainly of EntityManagerFactoryImpl and ...

    Hibernate体系结构的概要图

    Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。这6个核心接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务...

    hibernate3

    (2)SessionFactory:初始化Hibernate,充当数据存储源的代理,创建Session对象。一个SessinFactory实例对应一个数据存储源,应用从SessionFactory中获得Session实例。如果应用同时访问多个DB,怎需要为每个数据库...

    hibernate session.doc

    例如以下代码先加载一个持久化对象,然后通过delete()方法将它删除: Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer ...

    ACCP8.0北大青鸟SSH10章上机

    Configuration configuration = new ...或者通过MyEclipse的Hibernate工具自动生成的HibernateSessionFactory.getSession()方法来获取Session,调用HibernateSessionFactory. closeSession()方法来关闭session。

    利用Spring来管理Hibernate完整例子

    其中Hibernate每次都需要手动创建SessionFactory,Session,手动开启提交关闭事务。而这一切操作完全是由Spring来代替。使持久层更加方便,使开发人员减少持久层操作,把注意力放到业务上。

    hibernate操作数据库笔记

    使用Hibernate的Session对象操作数据库 1.初始化Hibernate:在要使用Hibernate的类的方法中实例化Configuration对象并用Configuration对象的configure()方法将hibernate.cfg.xml中的配置加载到内存,即: ...

    hibernate工具类

    hibernate开发的工具类,封装的sessionFactory,session等

    Hibernate3使用经验

    Query query = session.createQuery("from UserManager u where u.userName = :uname and u.password = :upsw"); query.setString("uname", uName); query.setString("upsw", psw); //根据参数位置设置参数,...

    Hibernate.zip

    hibernate,JDBC,SessionFactoryBuider,SessionFactory,Session的生命周期

    Hibernate入门(下)

    讲解程序的ORMapping框架——Hibernate,通过程序代码讲解Configuration、SessionFactory、Session在Hibernate中的主要作用

    HibernateUtil.java Hibernate5.2.1

    Hibernate5.2.1 的工具类 创建session 和 sessionFactory

    Hibernate快速入门

    本资源介绍了Hibernate组织架构,工作原理,配置方法、开发步骤,以及Configuration、SessionFactory、Session、Query、Transaction在Hibernate中的主要作用和使用方法,并资源以一个具体的例子贯穿所有内容;...

Global site tag (gtag.js) - Google Analytics