To skycncomp(^*-*^早知道今日的离别,你还愿意昨日的相逢吗)我去掉session.flush还是出现这种问题,另外,我的那个HibernateUtil代码是: /* * Created on 2005-10-18 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package all;/** * @author Jia Hailu * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;import net.sf.hibernate.*; import net.sf.hibernate.cfg.*;/** * @author Administrator * * TODO 要更改此生成的类型注释的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ public class HibernateUtil {
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s=(Session)session.get(); if (s == null) { if (sessionFactory == null) { try{ sessionFactory=new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { log.error("Initial SessionFactory creation failed.",ex); throw new ExceptionInInitializerError(ex); } } s = sessionFactory.openSession(); session.set(s); } return s; }
public static void closeSession() throws HibernateException { Session s=(Session) session.get(); session.set(null); if (s !=null) s.close(); }}
其中,我的Userlist.hbm.xml的代码如下:<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Sun Nov 13 23:20:11 CST 2005 --> <hibernate-mapping package="com.yourcompany.Hibernate"> <class name="Userlist" table="userlist"> <id name="id" column="Id" type="java.lang.String"> <generator class="uuid.hex"/> </id>
/*
* Created on 2005-10-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package all;/**
* @author Jia Hailu
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class HibernateUtil {
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory;
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s=(Session)session.get(); if (s == null) {
if (sessionFactory == null) {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
s = sessionFactory.openSession();
session.set(s);
} return s;
}
public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sun Nov 13 23:20:11 CST 2005 -->
<hibernate-mapping package="com.yourcompany.Hibernate"> <class name="Userlist" table="userlist">
<id name="id" column="Id" type="java.lang.String">
<generator class="uuid.hex"/>
</id>
<property name="username" column="userName" type="java.lang.String" not-null="true" />
<property name="userpwd" column="userPwd" type="java.lang.String" />
<property name="realname" column="realName" type="java.lang.String" />
<property name="birthday" column="birthday" type="java.util.Date" />
<property name="sex" column="sex" type="java.lang.String" />
<property name="company" column="company" type="java.lang.String" />
<property name="email" column="email" type="java.lang.String" not-null="true" />
<property name="phone" column="phone" type="java.lang.String" not-null="true" />
<property name="adress" column="adress" type="java.lang.String" not-null="true" />
<property name="usertype" column="usertype" type="java.lang.String" not-null="true"/>
</class>
</hibernate-mapping>