spring和hibernate整合使用getCurrentSession方法

来源:互联网 发布:查看端口是否的命令 编辑:程序博客网 时间:2024/06/06 07:16

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>

0 0
原创粉丝点击