日期:2014-05-18  浏览次数:20650 次

SSH整合
最近刚学SSH整合 struts2.3.14 hibernate4.3 spring3.2.2 
以下是使用sessionFactory.getCurrentSession().save(emp)错误提示 但是使用opensession()不会出错
Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
at com.sun.proxy.$Proxy8.save(Unknown Source)
at com.zhj.service.imp.EmployeeService.addEmployee(EmployeeService.java:23)
at com.zhj.test.MainTest.main(MainTest.java:50)

以下是配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
http://www.springframework.org/schema/context  
 http://www.springframework.org/schema/context/spring-context-3.2.xsd  
http://www.springframework.org/schema/aop   
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 启用注解扫描 -->
<context:annotation-config></context:annotation-config>

<bean id="testService" class="com.zhj.test.ServiceTest">
<property name="name">
<value>Test</value>
</property>
</bean>
<!-- 配置employeeService -->
<!-- 
<bean id="employeeService" class="com.zhj.service.imp.EmployeeService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 -->
<!-- 配置employeeService 通过注解方式-->
<bean id="employeeService" class="com.zhj.service.imp.EmployeeService">
</bean>
<!-- 配置departmentService 通过注解方式-->
<bean id="departmentService" class="com.zhj.service.imp.DepartmentService">
</bean>
<!-- 配置Action -->
<bean id="loginAction" class="com.zhj.action.LoginAction">
<property name="empInter" ref="employeeService"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssh?characterEncoding=UTF-8"></property>
<property name="username" value="root"></property>
<property name="password" value="1111"></property>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3"></property>
<!-- 连接池最大值 -->
<property name="maxActive" value="500"></property>
<!-- 最大空闲值 -->
<property name="maxIdle" value="2"></property>
<!-- 最小空闲值 -->
<property name="minIdle" value="1"></property>
</bea