hibernate4与3使用annotation在配置文件中的区别

让hibernate4与3在配置文件中的区别有几点:

1、获取sessionfactory的方式:

在hibernate3中获取sessionfactory的方式:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

在hibernate4中则是使用这种方式:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

2、事物的使用:

hibernate3:

<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">

hibernate4:

<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">

3、使用缓存机制:

hibernate3:

<prop key="hibernate.current_session_context_class">thread</prop>

hibernate4:

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

4、

在hibernate3中可以使用hibernate自带的Dao组件,如:HibernateTemplete/HibernateDaoSupport等;

在hibernate4中这些有取消了,必须使用getCurrentSession()来获取Session;

5        

   getCurrentSession()事务会自动关闭,所以在有所jsp页面查询数据都会关闭session。要想在jsp查询数据库需要加入:   org.springframework.orm.hibernate4.support.OpenSessionInViewFilter过滤器。

6、

Hibernate分页出现 ResultSet may only be accessed in a forward direction 需要设置hibernate结果集滚动

   <prop key="jdbc.use_scrollable_resultset">false</prop>

7、在单独使用hibernate4的使用:获取工厂的方式变为:

ServiceRegistry resgistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();

SessionFactory  factory = config.buildSessionFactory(resgistry);

Session session = factory.openSession();

当然你也可以通过设置单例的工具类来简便过去方式,具体工具类你可以查阅hibernate4的参考文档;

原文地址:https://www.cnblogs.com/yhiloon/p/3656218.html