<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/user"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!--这里开启了hibernate当中的线程session开关-->
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<!-- 使用包扫描方式添加类 -->
<property name="mappingResources">
<list>
<value>cn/hcong/vo/Person.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(public * cn.hcong.service.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
<bean id="personDao" class="cn.hcong.dao.impl.PersonDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="personService" class="cn.hcong.service.impl.PersonServiceImpl">
<property name="pd" ref="personDao"></property>
</bean>
然后调用了测试代码保存的代码,发现报Caused by: org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction,这样的错误,
原因就是将 <prop key="hibernate.current_session_context_class">thread</prop>这句话给删除掉就好了,