日期:2014-05-18  浏览次数:20793 次

hibernate无法往数据库添加记录,但能查询
用hibernate想往数据库添加记录,后台提示"添加记录成功!"!但数据库里的记录还是没有增加,而对数据库进行查询,删除操作却正常!实在找不到哪里出的问题!!

执行这一段代码往数据库增加记录(数据库用的是SQL SERVER)
Student stu = new Student();
stu.setId("111");
stu.setName("应");
stu.setUsername("beenin");
stu.setPassword("123456");
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.saveOrUpdate(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
System.out.println("添加记录成功!");

能正常执行到最后一句,但记录还是没有增加!

以下是详细代码,请高手们指点:
Student类:

public class Student implements java.io.Serializable {

  private String id;

  private String name;

  private String username;

  private String password;

  public Student() {
  }

  public Student(String name,String username, String password) {
  this.name = name;
  this.username = username;
  this.password = password;
  }

  // Property accessors

  public String getId() {
  return this.id;
  }

  public void setId(String id) {
  this.id = id;
  }

  public String getName() {
  return this.name;
  }

  public void setName(String name) {
  this.name = name;
  }

  public String getUsername() {
  return this.username;
  }

  public void setUsername(String username) {
  this.username = username;
  }

  public String getPassword() {
  return this.password;
  }

  public void setPassword(String password) {
  this.password = password;
  }

}

Student.hbm.xml:

<hibernate-mapping>
  <class name="com.gdms.domain.Student" table="student" lazy="false">
  <id name="id" type="java.lang.String">
  <column name="id" length="32" />
  <generator class="native" />
  </id>
  <property name="name" type="java.lang.String">
  <column name="name" length="32" />
  </property>
  <property name="username" type="java.lang.String">
  <column name="username" length="32" />
  </property>
  <property name="password" type="java.lang.String">
  <column name="password" length="32" />
  </property>  
  </class>
</hibernate-mapping>

HibernateUtil类:

package com.gdms.dao.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
  private static final SessionFactory sessionFactory;

  st