Getting duplicate entry errors from Hibernate, is MySQL to blame?

后端 未结 1 1638
臣服心动
臣服心动 2020-12-19 07:26

I am working on a database application which is mostly read-only, but there is one table which records user movement in the app and has a large number of writes to it. For

相关标签:
1条回答
  • 2020-12-19 07:39

    Increment is definitely bad if you have more than one process writing to the same table - you're bound to have collisions.

    Since it is MySQL we're talking about, the easiest thing to use would be identity. In your Hibernate mapping:

    <generator class="identity"/>
    

    In your MySQL script:

    CREATE TABLE IF NOT EXISTS `my_table` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `data1` int(11) NOT NULL,
      `data2` int(11) NOT NULL,
      `timestamp` datetime default NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
    

    To alter an existing table:

    ALTER TABLE `my_table`
      CHANGE COLUMN `id` `id` int(11) NOT NULL AUTO_INCREMENT=$NEW_VALUE$;
    

    where $NEW_VALUE$ should be replaced by the next available id so that sequence does not reset to 1.

    0 讨论(0)
提交回复
热议问题