Fw: mysql自增长的主键Duplicate错误

  |   0 评论   |   997 浏览

     
    mysql自增长的主键Duplicate错误
    hibernate3.1
    mysql5.1.16
    映射文件 配置主键生成策略
    <id   name="userid"   column="userid"   type="int">   
      <generator   class="increment"/>   
     </id> 
     
    保存数据的时候有时候会报  主键重复的异常
    userid现在的值是399
    报的异常是   
     
     
    Java代码  
     SQL Error: 1062, SQLState: 23000  
    10:02:15 ERROR JDBCExceptionReporter - Duplicate entry '321' for key 1  
    10:02:15 ERROR AbstractFlushingEventListener - Could not synchronize database state with session   
    org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update   
            at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)   
            at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)   
            at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)   
            at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)   
            at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)   
            at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)   
            at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)   
            at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)   
            at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)   
            at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)   
            at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)   
            at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)   
            
     SQL Error: 1062, SQLState: 23000
    10:02:15 ERROR JDBCExceptionReporter - Duplicate entry '321' for key 1
    10:02:15 ERROR AbstractFlushingEventListener - Could not synchronize database state with session
    org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
            at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
            at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
            at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
            at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
            at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
            at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
            at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
            at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
            at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
            at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
            at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
            at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
          
     
    是什么问题呢,想了我一个晚上,没想到是哪里的问题
    难道是native的问题?
    数据库的问题?
    程序的问题?
     
    Java代码  
    increment   适用于代理主键。由Hibernate自动以递增方式生成      
    identity    适用于代理主键。由底层数据库生成标识符      
    sequence    适用于代理主键。Hibernate根据底层数据库的序列生成标识符,这要求底层数据库支持序列      
    hilo      适用于代理主键。Hibernate分局high/low算法生成标识符      
    seqhilo     适用于代理主键。使用一个高/低位算法来高效的生成long,short或者int类型的标识符。      
    native     适用于代理主键。根据底层数据库对自动生成标识符的方式,自动选择identity、sequence或hilo      
    uuid.hex    适用于代理主键。Hibernate采用128位的UUID算法生成标识符      
    uuid.string     适用于代理主键。UUID被编码成一个16字符长的字符串      
    assigned    适用于自然主键。由Java应用程序负责生成标识符      
    foreign     适用于代理主键。使用另外一个相关联的对象的标识符   
    increment   适用于代理主键。由Hibernate自动以递增方式生成   
    identity    适用于代理主键。由底层数据库生成标识符   
    sequence    适用于代理主键。Hibernate根据底层数据库的序列生成标识符,这要求底层数据库支持序列   
    hilo      适用于代理主键。Hibernate分局high/low算法生成标识符   
    seqhilo     适用于代理主键。使用一个高/低位算法来高效的生成long,short或者int类型的标识符。   
    native     适用于代理主键。根据底层数据库对自动生成标识符的方式,自动选择identity、sequence或hilo   
    uuid.hex    适用于代理主键。Hibernate采用128位的UUID算法生成标识符   
    uuid.string     适用于代理主键。UUID被编码成一个16字符长的字符串   
    assigned    适用于自然主键。由Java应用程序负责生成标识符   
    foreign     适用于代理主键。使用另外一个相关联的对象的标识符 刚才看了一下日志  报错的对象都是这样的设置<generator class="increment" />
    根据hibernate手册的描述,increment是由hibernate来完成自增长的,
    increment主键生成器的org.hibernate.id.IncrementGenerator是使用select max( columnName ) from tableName的方式来获取,做应用负载出现问题是必然的。
    所以应该杜绝increment的使用。
    主键建议使用UUID的方式生成。

    评论

    发表评论

    validate