本文主要是介绍spring和hibernate整合使用getCurrentSession方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
spring和hibernate整合使用getCurrentSession()方法获得session实例时,一定记得在sessionFactory的bean中添加<prop key="hibernate.current_session_context_class">thread</prop>,如果你的事务时交给spring管理的,则就不要配置,不然就会报错:the save is not valid without active transaction.详细参见:http://blog.csdn.net/irelandken/article/details/7193123 这篇博客。
完整代码如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop> <!-- 使用getCurrentSession()方法,必须配置此属性 -->
</props>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/pojo/CirFriends.hbm.xml</value>
<value>com/hibernate/pojo/CirInformation.hbm.xml</value>
<value>com/hibernate/pojo/CirUser.hbm.xml</value>
<value>com/hibernate/pojo/CirRemark.hbm.xml</value></list>
</property>
</bean>
这篇关于spring和hibernate整合使用getCurrentSession方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!