在你第一次运行时你的主键数据库中是没有的
第二次你的主键已经存在了,所以出所不能执行jdbc你把session.flush去掉,直接commit试试看

解决方案 »

  1.   

    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 {

    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();
      }}
      

  2.   

    其中,我的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>
     
            <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>
      

  3.   

    我的问题解决了,谢谢,uuid.hex是产生32位的字符串,而我数据库是varchar(30)的,所以在加入第一次数据时,字符串给截断加入到数据库中,第二次产生的字符串被截断后与前一条记录完全一致,所以造成不能新增数据。