MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » hibernate无法往数据库添加记录,但能查询,该如何处理

hibernate无法往数据库添加记录,但能查询,该如何处理

www.MyException.Cn  网友分享于:2013-01-20  浏览:20次
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;

  static {
  try {
  // Create the SessionFactory
  sessionFactory = new Configuration().configure().buildSessionFactory();

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有