是不是那个原因啊,你的hibernate是纳入在spirng的事务中,是不是要把 spring的事务也设置成不让它自动提交
如果说不行的话就没办法了
先帮你顶了
up!
up!up!

解决方案 »

  1.   

    我没有用到事务。
    发现 如果把数据源写在Hibernate配置文件中,会有不同的结果:
    System.out.println(session.connection().getAutoCommit()); //结果为false,与上面不同
    //session.beginTransaction().commit();  //数据不会提交到数据库中,除非去掉注释,与上面不同
    Hibernate配置文件:
    <hibernate-configuration>
    <session-factory>
    <property name="connection.username">root</property>
    <property name="connection.url">
    jdbc:mysql://localhost:3306/mldn
    </property>
    <property name="dialect">
    org.hibernate.dialect.MySQLDialect
    </property>
    <property name="myeclipse.connection.profile">MySQL</property>
    <property name="connection.password">root</property>
    <property name="connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="show_sql">true</property>
    <property name="connection.autocommit">false</property> <!-- 在Hibernate中默认就是false -->
    <mapping resource="vo/Student.hbm.xml" /></session-factory>
    </hibernate-configuration>Spring配置文件:<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
    </property>
    </bean><bean id="studentDao" class="vo.StudentDaoImpl">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>测试类:
    public class Test {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentDao studentDao = (StudentDao)context.getBean("studentDao");
    Student student = new Student();
    student.setName("测试");
    studentDao.save(student);
            }
    }
      

  2.   

    你是说会有2种情况?
    那会不会是spring默认提交他了?
      

  3.   

    我记得如果将hibernate.connection.autocommit设置成false的话,必须要写commit(),否则虽然sql语句会打印出来,但是是不会提交的呀等我有时间测试一下你的这个先帮你up
      

  4.   

    Spring中已经配置<prop key="hibernate.connection.autocommit">false</prop> ,没有配置任何事务
    但数据会自动提交到数据库。
    我就是这点不明白。:(
      

  5.   

    Transaction t = session.beginTransaction();
      

  6.   

      <bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource">
    这个数据源的有个属性是: defaultAutoCommit:设置从数据源中返回的连接是否采用自动提交机制,默认值为 true; 
    应该把这个设置成false
      

  7.   

    在hibernate.cfg.xml中增加一条配置信息
    <property name="connection.autocommit">true</property>