您的位置:首页 > 编程语言 > Java开发

spring和hibernate整合使用getCurrentSession方法

2016-03-28 20:34 441 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: