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();
}catch (Throwable ex) {
ex.printStackTrace();
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();
@SuppressWarnings("unchecked")
public static Session currentSession() {
Session session = (Session) tLocalsess.get();
// open a new one, if none can be found.
try {
if (session == null || !session.isOpen()) {
session = openSession();
tLocalsess.set(session);
}
} catch (HibernateException e) {
// throw new HibernateException(e);
e.printStackTrace();
}
return session;
}
@SuppressWarnings("unchecked")
public static void closeSession() {
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try {
if (session != null && session.isOpen()) {
session.close();
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
@SuppressWarnings("unchecked")
public static void beginTransaction() {
System.out.println("begin Transaction");
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx == null) {
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
@SuppressWarnings("unchecked")
public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
System.out.println("commit tx");
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
@SuppressWarnings("unchecked")
public static void rollbackTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
private static Session openSession() throws HibernateException {
return getSessionFactory().openSession();
}
private static SessionFactory getSessionFactory() throws HibernateException {
return sessionFactory;// SingletonSessionFactory.getInstance();
}
}
还有是数据库的student表:
[解决办法]
还有你id是String的话id的生成策略换成assigned试试。
楼上的说stu.setId("111"); 不可以。其实也不是绝对,只要表里的id不存在这个,其实也是可以设进去的。
[解决办法]
我看你代码好像把e.printStace();给注释掉了?