最近在用SSH做个毕业设计。在使用Hibernate与Spring结合时出现了问题。每次使用HibernateTemplate.save()方法,数据库中相对应的表就会被清空,然后再插入那条新记录。而如果当当使用Hibernate本身的session保存就不会出问题,这个问题郁闷了几天。望有人能给与解答。代码如下。DAOSupportpackage dao;import org.springframework.orm.hibernate3.HibernateTemplate;public class DAOSupport {
    protected HibernateTemplate template;
    
    public DAOSupport(HibernateTemplate template) {
        this.template = template;
    }
}BlogUserDAOpackage dao.interfaces;import entity.BlogUser;public interface BlogUserDAO {
    public void save(BlogUser user);
    public boolean exists(BlogUser user);    public String getPasswordMD5(BlogUser user);
}
BlogUserDAOImpl/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;import dao.interfaces.BlogUserDAO;
import entity.BlogUser;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;public class BlogUserDAOImpl extends DAOSupport implements BlogUserDAO{    public BlogUserDAOImpl(HibernateTemplate template) {
        super(template);
    }
    
    @Override
    public void save(BlogUser user) {
        template.save(user);
    }    @Override
    public boolean exists(BlogUser user) {
        return (getPasswordMD5(user) != null) ? true : false;
    }
    
    @Override
    public String getPasswordMD5(BlogUser user) {
        String hql = "select passwordMd5 from BlogUser where user = ?";
        
        List<String> passwordMD5 = template.find(hql, user);
        System.out.println(passwordMD5);
        if(passwordMD5.size() > 0)
            return passwordMD5.get(0);
        return null;
    }
}Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/blog"/>
       <property name="username" value="root"/>
       <property name="password" value="123"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
       <property name="mappingResources">
           <list>
               <value>entity/BlogUser.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>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="blogUserDAO" class="dao.BlogUserDAOImpl">
        <constructor-arg><ref bean="hibernateTemplate"/></constructor-arg>
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    </beans>

测试类

package dao;import dao.interfaces.BlogUserDAO;
import entity.BlogUser;
import java.util.Date;
import org.junit.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;public class BlogUserDAOImplTest {
    BlogUser user;
    ApplicationContext ctx;
    
    public BlogUserDAOImplTest() {
    }    @BeforeClass
    public static void setUpClass() throws Exception {
    }    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
        System.out.println("setUp");
        
        user = new BlogUser("ssss23", "sss", new Date());
        
    }
    
    @After
    public void tearDown() {
    }    /**
     * Test of save method, of class BlogUserDAOImpl.
     */
    @Test
    public void testSave() {
        System.out.println("save");
        //用这种方法就没问题
//        Session session = HibernateUtil.getSessionFactory().openSession();
//        Transaction tx = null;
//        try {
//            tx = session.beginTransaction();
//            session.save(user);
//            tx.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            session.close();
//        }        //用这种方法是就会出现清空表在插入的问题
        ctx = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml");
        BlogUserDAO blogUserDAO = (BlogUserDAO)ctx.getBean("blogUserDAO");
        //user = new BlogUser("admin", "admin", new Date());
//        user2 = new BlogUser("test213", "test3", new Date());
        blogUserDAO.save(user);
        // TODO review the generated test code and remove the default call to fail.
//        fail("The test case is a prototype.");
    }//    @Test
//    public void testGetPasswordMD5() {
//        BlogUserDAO blogUserDAO = (BlogUserDAO)ctx.getBean("blogUserDAO");
//        System.out.println("getPasswordMD5");
//        String password = blogUserDAO.getPasswordMD5(user);
//        assertNotSame(user.getPasswordMd5(), password);
//    }
    
    /**
     * Test of exists method, of class BlogUserDAOImpl.
     */
//    @Test
//    public void testExists() {
//       // BlogUserDAO blogUserDAO = new BlogUserDAOImpl(
//         //       new HibernateTemplate(HibernateUtil.getSessionFactory()));
//        System.out.println("exists");
//        //assertTrue(blogUserDAO.exists(user));
//    }
}
问大伙有知道此问题原因的给予解答。