HibernateDaoSupport中delete方法无效,单元测试有效
本帖最后由 wxx19910319 于 2013-04-14 12:26:54 编辑 如题,直接贴上配置文件把
spring的核心配置文件beans.xml
<bean id="orgManager" class="com.bjsxt.oa.manager.impl.OrgManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="org" class="com.bjsxt.oa.web.OrgAction" scope="prototype">
<property name="orgManager" ref="orgManager" />
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/bjsxt/oa/model/Orgnization.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.bjsxt.oa.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
web.xml配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>openSessionInView</filter-name>
<!-- The default name of sessionfactory is "sessionfactory", but you can change it here. Reference it's source -->
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这个是OrgManagerImpl里面的方法,就是这个方法从前台的浏览器不能删除机构,但是单元测试可以删除,初步怀疑是Session没有提交,可是spring里面事务也都配置过了啊,为什么还是不行呢
?OpenSessionInViewFilter有没有可能是这个的问题
public void delOrg(int orgId) {
if (getHibernateTemplate().find("from Orgnization o where o.parent.id = " + orgId).size() != 0) {
throw new RuntimeException("该机构有子机构,不能删除");
}
getHibernateTemplate().delete(
getHibernateTemplate().load(Orgnization.class, orgId)
);
}
经过刚刚又测试了测试了一下,update方法也无效,只有find、save方法可用,可以发出select、insert语句,就是发不出update、delete语句。
单元测试都通过了,就是前台提交过来的不行啊 HibernateDaoSupport delete方法 无效 单元测试通过
[解决办法]
如果使用OpenSessionInView,那其filter或者Interceptor应该在相应的Action前面,
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
当然你也可以修改一下,只过滤相应的.action或其它.
再者你的template.load改成get方法试一下呢.
[解决办法]
没有报任何异常的原因可能是 throw new RuntimeException("该机构有子机构,不能删除");
这段代码的原因,极有可能是if判断的时候出的问题,所以你把这段代码暂时去掉,用try catch捕获把栈的运行轨迹打印出来,。因为你直接throw即使有异常控制台也没有任何信息,因为没有e.printStackTrace();
你试试我说的应该可以找到你的错误原因在哪里,然后再慢慢分析,