打印

[Java] 关于使用SPRING和HIBERNATE的奇怪问题

关于使用SPRING和HIBERNATE的奇怪问题

我是使用的MYSQL数据,往数据添加数据,使用SPRING框架中的HibernateDaoSupport
我的一个实体MESSAGE已经存好数据了,使用
gethibernatetemplate.save(message);向数据库中插入新数据,但是不是单纯的无法插入,我数据库中有一个自动增量的主键ID,虽然插入数据后查看数据库没有看到数据,但是ID还是增加了1(我从数据库中再次直接插入新数据时发现ID隔了一个数字所以才发现的);我一开始一直认为数据库没有数据是因为我没有COMMIT,但是我看了一下发现SAVE是自动提交的,而且数据库里边的ID增加了1说明是有数据的,但是我就是看不到数据


但是我使用
Session session=getSession();
Trasction tx=session.begintrasction();
session.save(message);
tx.commit();
可以正常插入数据,让我感到很奇怪


有些命令的拼写有点问题请大家见谅,但编程的时候保证是对的。多谢大家了

TOP

估计你上面的spring没有把事务配置好,
Session session=getSession();
Trasction tx=session.begintrasction();
session.save(message);
tx.commit();这里使用了事务当然能够成功提交
上面没有配置事务所以不能提交成功,但是id还是能够增长的

TOP

那是不是要在spring的配置文件中配置事务的代理吗?能否在代码中直接加入事务的处理过程呢?如果可以的话如何加呢,谢谢!

TOP

可以在代码中直接写事务处理的语句,每个操作数据库的地方都要写,既然用了spring就应该使用他的aop来管理事务,很方便的,上网搜一下有很多

TOP

谢谢了,我试了一下,按照一本书上的例子,出现如下的问题,有些长,但是比较清晰
加上的事务配置
<!-- 下边是事务的配置 -->       
<bean id="transctionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernatefactory"></property>
</bean>
<bean id="transctioninterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transctionManager"></property>
<property name="transactionAttributes">
<props>
  <prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>


<!-- 下边是service配置 -->       
<bean name="myservice" class="myservice.MyService">
<property name="bulletinDAO" ref="bulletindao"></property>
<property name="pictureDAO" ref="picturedao"></property>
<property name="priceDAO" ref="pricedao"></property>
<property name="searchDAO" ref="searchdao"></property>
<property name="messageDAO" ref="messagedao"></property>
</bean>       


<!-- 定义BeanNameAutoProxyCreator -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>myservice</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transctioninterceptor</value>
</list>
</property>
</bean>

我用的是SPRING代理STRUTS的ACTION,不加事务之前运行正常,加了之后就报错说是无法创建ACTION了
我的ACTION映射

<!-- 下边是action的映射 -->
<bean name="actionbase" class="com.yourcompany.struts.action.BaseAction" abstract="true">
<property name="service" ref="myservice"></property>
</bean>
<bean name="/showpicture" class="com.yourcompany.struts.action.ShowpictureAction" scope="prototype" parent="actionbase"></bean>
<bean name="/showprice" class="com.yourcompany.struts.action.ShowpriceAction" scope="prototype" parent="actionbase"></bean>
<bean name="/message" class="com.yourcompany.struts.action.MessageAction" scope="prototype" parent="actionbase"></bean>
<bean name="/search" class="com.yourcompany.struts.action.SearchAction" scope="prototype" parent="actionbase"></bean>


报错信息
严重: Servlet.service() for servlet action threw exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/message' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy7] to required type [myservice.MyService] for property 'service'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy7] to required type [myservice.MyService] for property 'service': no matching editors or conversion strategy found
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy7] to required type [myservice.MyService] for property 'service'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy7] to required type [myservice.MyService] for property 'service': no matching editors or conversion strategy found
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy7] to required type [myservice.MyService] for property 'service': no matching editors or conversion strategy found
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:815)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:645)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1127)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:862)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:424)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:270)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:737)
        at org.springframework.web.struts.DelegatingRequestProcessor.getDelegateAction(DelegatingRequestProcessor.java:168)
        at org.springframework.web.struts.DelegatingRequestProcessor.processActionCreate(DelegatingRequestProcessor.java:146)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:221)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
        at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Unknown Source)

[ 本帖最后由 zhjdenis 于 2008-6-5 18:10 编辑 ]

TOP

创建message这个bean时出错了,看看struts-config里面的配置吧

TOP

谢谢,struts-config应该没有问题,因为不加入事务处理的时候一切都是正常

TOP

为什么啊,谢谢大家了

TOP

希望高手帮帮我,谢谢了

TOP

org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy7] to required type [myservice.MyService] for property 'service'

检查下com.yourcompany.struts.action.MessageAction 类里有没有service属性,
与用Spring注入的service是不是类型一致。

TOP


感谢一直以来您对我们的支持!
当前时区 GMT+8, 现在时间是 2008-12-3 08:42 京ICP证060528 号

Designed By 17DST