首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

SSH整合,该如何处理

2013-10-21 
SSH整合最近刚学SSH整合 struts2.3.14 hibernate4.3 spring3.2.2 以下是使用sessionFactory.getCurrentSes

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>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>


<!-- 接管hibernate对象映射文件 -->
<property name="mappingResources">
<list>
<value>com/zhj/domain/Employee.hbm.xml</value>
<value>com/zhj/domain/Department.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<value>
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto = update
hibernate.show_sql = true
hibernate.current_session_context_class = thread
</value>
</property>
</bean>
</beans>
还有一个问题 spring的jar包里是不是已经自动解决了懒加载的问题啊 我没配什么 也没写别的代码 懒加载没影响啊 SSH Spring
[解决办法]
就是要引入TransactionManager,并将你的addEmployee方法置于事务控制之下再试试
[解决办法]
<!-- 设置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="NOT_SUPPORTED"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor
pointcut="execution(* com.bysee.ssh2.service.*.*(..))"
advice-ref="txAdvice" />
</aop:config>
类似于这样写

热点排行