ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DraftDAO dao = (DraftDAO)ctx.getBean("DraftDAO");
其中DraftDAO是spring和hibernate整合,根据数据库表逆向生成的。
System.out.println("新增一篇新文章的草稿!");
Draft draft = new Draft();
Date utilDate = new Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
draft.setDraftCreate(sqlDate);
draft.setDraftTitle(title);
draft.setDraftType(type);
draft.setDraftInfo(describe);
draft.setCont(stemTxt);
System.out.println(555555);
dao.save(draft); //执行到输出55555,这一句就没有执行了 ,也没有保存到数据库
System.out.println(111122333); //没有输出这个了?
这是怎么回事呢?其他的action是可以保存到数据库的,就这个action不可以。
可能是没有配置事务。
打断点看一下DraftDAO dao = (DraftDAO)ctx.getBean("DraftDAO");这个dao是不是null
如果是null就是没有注入成功,解决方法:
1是把DraftDAO dao = (DraftDAO)ctx.getBean("DraftDAO");括号里的第一个D改成小写,DraftDao改成draftDao
2是把ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");括号里改成classpath:applicationContext.xml
你看一下你其他action是怎么管理事务的。比对一下。如果你的bean是注入了的。而数据没有提交的话,很有可能就是事务的问题。你先按2L的方法判断bean有没有注入。
事务的配置
< bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
< property name="sessionFactory">
< ref local="sessionFactory" />
< /property>
< /bean>
< !-- 声明式事务管理 -->
< bean id="proxyTemplate"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
< property name="transactionManager">
< ref bean="transactionManager" />
< /property>
< property name="transactionAttributes">
< props>
< prop key="save*">PROPAGATION_REQUIRED
< prop key="query*">PROPAGATION_REQUIRED,readOnly
< /props>
< /property>
< /bean>
< bean id="nameproxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
< property name="beanNames">
< list>
< value>DraftDao
< /list>
< /property>
< property name="interceptorNames">
< list>
< value>proxyTemplate< /value>
< /list>
< /property>
< /bean>
你在好好研究下你的其他类,和这个类的区别。我也是个菜鸟。解决了分享下你的经验。