SpringMVC3.2+hibernate4注解形式保存数据无效 在save外面加一个try catch,把错误打印出来看一下。以前也碰到过,没有捕获的话不会报异常,然后就是不能插入到数据库。你试一下是不是这个原因。或者设置断点,调试一下,看一下执行save的时候有没有报错误。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有无事务?service层那个方法要不要加事务? 既然用注解,把 <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED</prop> <prop key="execute*">PROPAGATION_REQUIRED</prop> <prop key="load*">PROPAGATION_REQUIRED</prop> <prop key="merge*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>改为 <!-- 注入事务 --> <tx:annotation-driven transaction-manager="transactionManager" />再加入 <!-- 自动扫描 bean --> <context:annotation-config /> <context:component-scan base-package="zzz.xxx"/>service上加@Transactional@Transactional@Service("teacherService")public class TeacherService { 多半是事务引起的,我用的是aop你试试看我的代码<context:component-scan base-package="com.share.service.*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven proxy-target-class="true" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="search*" read-only="true" propagation="REQUIRED" /> <tx:method name="isExist*" read-only="true" propagation="REQUIRED" /> <tx:method name="load*" read-only="true" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(public * com.share.service..*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> 还是别用这种方式控制事务了,spring这块有严重问题,对同一个表读写频繁很容易锁死数据源。 我用session.flush()就没有问题了。 楼主,我和你一样的问题,只有save方法不起作用,后面要跟上flush方法才起效,楼主怎么解决的? 我的也是这问题,事务控制应该没问题,因为修改和删除都正常,就只有这个save没效果,增加了flush后竟然还报错了,但是hibernate往数据库插入数据的语句显示出来了,但数据库还是没数据 问一下这里只是扫描了<context:component-scan base-package="com.share.service.*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>的注解,其他注解配置在哪里配 hibernate 一对一,无法级联保存。 关于session生命周期的一点疑问. 初学hibernate 几个基础问题 求助 tomcat调用ffmpeg.exe的问题 用java开发一套web的进销存大概要多少钱??需要多少时间?? 被调用asp回传URL地址的问题 关于jsp中的request的问题 如何改变JSP中的值 如何解决:include一个带参数的JSP文件动? PoolMan怎样配置啊,有没有人会啊??? 同一台机器挂载多个tomcat时 session的问题 java开发的服务端能不能调用客户端的本地程序?
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED</prop>
<prop key="execute*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
改为
<!-- 注入事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />再加入
<!-- 自动扫描 bean -->
<context:annotation-config />
<context:component-scan base-package="zzz.xxx"/>service上加@Transactional@Transactional
@Service("teacherService")
public class TeacherService {
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <tx:annotation-driven proxy-target-class="true" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="search*" read-only="true" propagation="REQUIRED" />
<tx:method name="isExist*" read-only="true" propagation="REQUIRED" />
<tx:method name="load*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution(public * com.share.service..*.*(..))"
id="bussinessService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
</aop:config>
<context:component-scan base-package="com.share.service.*">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
的注解,其他注解配置在哪里配