我的Spring配置文件如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property> <property name="url">
<value>jdbc:mysql://localhost:3306/store?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property> <property name="username">
<value>root</value>
</property> <property name="password">
<value>****</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property> <property name="mappingResources">
<list>
<value>com/**/bean/ReceUnit.hbm.xml</value>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop> <prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="ReceUnitDAO" class="com.**.dao.impl.ReceUnitDAOImpl"
scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="receUnitServiceTarger" class="com.**.service.impl.ReceUnitServiceImpl">
<property name="receUnitDAO">
<ref bean="ReceUnitDAO" />
</property>
</bean>

<bean id="receUnitService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="receUnitServiceTarger"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><!-- PROPAGATION_REQUIRED:如果存在一个事务就使用这个事务,否则开启新的事务 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <bean id="addReceUnit" class="com.**.action.receunit.AddReceUnit">
<property name="service" ref="receUnitService"></property>
</bean>Bean如下:
public class ReceUnit {
private Integer id;
private String index;
private String name;
        setter()&&getter();
}Action(属性驱动)如下:public class AddReceUnit extends ActionSupport {
private String index;
private String name;
private ReceUnitService service;
        setter()&&getter();
        
        @Override
public String execute() throws Exception {

                ReceUnit ru = new ReceUnit();
                //主键id在hbm中设置为自增类型
ru.setIndex(index);
ru.setName(name);
this.service.insert(ru);
return SUCCESS;
}中间的ReceUnitService、ReceUnitServiceImpl和ReceUnitDAO就省略了。DAOImpl代码如下:public class ReceUnitDAOImpl extends HavenDAOImpl implements ReceUnitDAO {

@Override
public void insert(ReceUnit ru) {
this.getHibernateTemplate().save(ru);
}}hbm如下:
<hibernate-mapping>
 <class name="com.fanghongyu.bean.ReceUnit" table="rece_unit">
  <id name="id" column="id" type="integer">
   <generator class="increment">
    <!-- 主键设置为自增类型 -->
   </generator>
  </id>
  <property name="index" column="index" type="string"></property>
  <property name="name" column="name" type="string"></property>
 </class>
</hibernate-mapping>执行代码时,控制台有如下的现实:
Hibernate: select max(id) from rece_unit
Hibernate: insert into rece_unit (index, name, id) values (?, ?, ?)
但是看数据库中没有插入成功,请问为什么?
另外我是异步提交的就是:
$.ajax({
   type : "GET",
   url : "addReceUnit.action",
   dateType : "html",
   data : {"index" : $("#rece_unit_index").val(), "name" : content},
   success : function(returnedData){
   }
   });
不知是不是这里出了问题,但是插入前通过控制台可以看到值是拿到了。

解决方案 »

  1.   

    希望有人能够耐心一点把帖子看完教教我,我卡了快一天了....我觉得spring的配置应该没有问题吧,以前插入都很正常,是异步提交导致的问题么?
      

  2.   

    应该url : "addReceUnit.action",不对你可以用DEBUG看看,有没有进入ACTION里面去
      

  3.   

    设置断点调试下吧,首先看到action没 ,然后看到service没有,在就是dao方法调用的时候。
      

  4.   

    都打印出了:
    Hibernate: select max(id) from rece_unit
    Hibernate: insert into rece_unit (index, name, id) values (?, ?, ?)
    都进入了DAOImpl了
      

  5.   

    将dao重新生成下吧,这个错误应该是指跟数据库连接的问题,还有你的web.xml里面的list加了么?
      

  6.   

    web.xml只写了
    <filter>
    <filter-name>store</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter> <filter-mapping>
    <filter-name>store</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    请问list是什么,原来没有加过也可以插入啊我的DAO如下:
    public interface ReceUnitDAO {
    public void insert(ReceUnit ru);
    }我觉得数据库已经连上了,其他页面都可以正常的对数据库进行增删查改
      

  7.   

    你debug的时候有没有值?然后就是把你保存的那段代码贴出来。
      

  8.   

    是所有的添加方法都这样还是只有这个方法不行。所有的都不行看看是不是驱动或少包 。只有这个的话 建议你写个mian方法测试一下
      

  9.   

    说错了 不是web.xml里的是applicationContext.xml里面的。 
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>school/model/User.hbm.xml</value>
    <value>school/model/Teacher.hbm.xml</value>
    <value>school/model/BookCase.hbm.xml</value>
    <value>school/model/Program.hbm.xml</value>
    <value>school/model/Award.hbm.xml</value>
    </list>
    </property>
    </bean>
    就像这个样子有个注入。你把错误提示信息贴出来。
      

  10.   


    看看service层,估计有异常回滚了撒,在service层,执行操作后,打印条信心看看有没有啊
      

  11.   

    看看你的数据表是不是有什么非空的设置,而你传进去的值有null
      

  12.   

    <!-- 自动提交 -->
    <prop key="hibernate.connection.autocommit">true</prop>
      

  13.   

    没有错误信息,控制台上打印了insert了,但是数据库中没有插入
      

  14.   

    以前有人提到过这种问题,基本都是事务配置的问题:
    <property name="transactionAttributes">
    <props>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><!-- PROPAGATION_REQUIRED:如果存在一个事务就使用这个事务,否则开启新的事务 -->
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    你这个不觉的涵盖面太广了吗?你想把红色部分的这一句去掉试试看可以不/
      

  15.   

    你将事务加在dao中,
    this.getHibernateTemplate().save(ru);
      

  16.   

    我找到错误了,数据库中的字段名起了“index”这个关键字,在hbm文件中映射时index对应的column应加反引号(笔记本键盘上1左边的符号)。由于用可视化界面建的表,建表时自动加上了反引号,所以没有注意到用了关键字。谢谢了!
      

  17.   

    我找到错误了,数据库中的字段名起了“index”这个关键字,在hbm文件中映射时index对应的column应加反引号(笔记本键盘上1左边的符号)。由于用可视化界面建的表,建表时自动加上了反引号,所以没有注意到用了关键字。谢谢了!