测试用的servlet代码如下:
public class TestHBMServlet extends HttpServlet {  private static Logger log = Logger.getLogger(TestHBMServlet.class);
  public void init() throws ServletException {
    super.init();
    Configuration cfg = null;
    SessionFactory sf = null;
    Session s = null;
    try {
      cfg = new Configuration().configure();
    }catch (MappingException e1) {
      e1.printStackTrace();
      log.error("映射错误:" + e1);
      return;
    }catch (HibernateException e) {
      e.printStackTrace();
      log.error("映射错误:" + e);
      return;
    }    try {
      sf = cfg.buildSessionFactory();
      s = sf.openSession();
    }catch (HibernateException e) {
      e.printStackTrace();
      log.error("Hibernate 错误" + e);
      return;
    }    try {
      log.info("Open Session OK");
      long start = System.currentTimeMillis();
      for (int i = 0; i < 10; i++) {
        Userlist message = new Userlist();
        message.setId(String.valueOf(i));
        message.setUsername(String.valueOf(i));
        message.setPassword(String.valueOf(i));
        try {
          s.save(message);
          s.flush();
        }
        catch (HibernateException e4) {
          e4.printStackTrace();
          log.error("Hibernate Insert1 错误" + e4);
          return;
        }
      }
      long end = System.currentTimeMillis();
      log.info(new Long(end - start));
    }catch (Exception e) {
      e.printStackTrace();
      log.error("Hibernate Insert2 错误" + e);
      return;
    }finally {
      try {
        s.close();
      }catch (HibernateException e4) {
        e4.printStackTrace();
        log.error("Hibernate Close 错误" + e4);
      }
    }
  }
}            }
            catch (HibernateException e4) {
              e4.printStackTrace();
              log.error("Hibernate Insert1 错误" + e4);
              return;
            }
    }
          long end = System.currentTimeMillis();
          log.info(new Long(end - start));
        }catch (Exception e) {
          e.printStackTrace();
          log.error("Hibernate Insert2 错误" + e);
          return;
        }finally {
          try {
            s.close();
          }catch (HibernateException e4) {
            e4.printStackTrace();
            log.error("Hibernate Close 错误" + e4);
          }
        }
userlist表的结构为:
SQL> desc userlist;
 名称                                      空?      类型
 ----------------------------------------- -------- ----------------------- USERID                                    NOT NULL VARCHAR2(10)
 USERNAME                                  NOT NULL VARCHAR2(20)
 PASSWORD                                  NOT NULL VARCHAR2(20)

解决方案 »

  1.   

    userlist.hbm.xml如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
    <class name="struts.hibernate.Userlist" schema="HYH" table="USERLIST">
    <id column="USERID" length="10" name="id" type="string">
    <generator class="uuid.hex"/>
    </id>
    <property column="USERNAME" length="20" name="username" not-null="true" type="string"/>
    <property column="PASSWORD" length="20" name="password" not-null="true" type="string"/>
    </class>
    </hibernate-mapping>
      

  2.   

    你的mapping文件呢?帖出来看看
      

  3.   

    <generator class="uuid.hex"/>]uuid.hex返回是32位的编码....试试把数据库的ID字段扩大到32以上或者用别的generator  比如<generator class="assigned"/>
      

  4.   

    多谢 bluemeteor(挂月||╭∩╮(︶_︶)╭∩╮) 兄弟。问题解决!
    还想请教一下<generator class="uuid.hex"/>与<generator class="assigned"/>的区别,什么时候用到哪一种?马上给分!
      

  5.   

    uuid.hex 是通过UUID算法生成字符串...算法和JVM直接挂钩...生成字符串有很大的唯一性...相对于别的generator选项...uuid适合有各大数据库assigned 是程序指定ID的值...也就是说..你的主键值是JAVA程序生成的...我不推荐你使用这种方法:)你用oracle数据库还可以使用sequence...这些在hibernate的文档里面都有..说了半天那里有更加权威官方的解释:)  hibernate的2.1.4以后版本都有中文文档了....good luck