简体   繁体   English

休眠错误生成自动ID增量

[英]Hibernate wrong generate auto id increment

I am not sure what happened but one day Hibernate started to wrong generate auto ID in Osoba. 我不确定发生了什么,但是有一天,Hibernate开始错误地在Osoba中生成自动ID。

What I mean is when I add person(Osoba) he gets ID 60, next one gets 64 but it should get 61... How can I fix that? 我的意思是,当我添加人员(Osoba)时,他的ID为60,下一位的ID为64,但应该为61。我该如何解决? So it will increment by 1 no by 4 or other like this is now... Also how can I "reset" ID value? 因此它会以1 no by 4或其他方式递增,就像现在这样...另外,如何“重置” ID值? I would like to clean whole table, and start counting from ID 1 我想清理整张桌子,并从ID 1开始计数

<class name="Osoba" table="DANEOSOBOWE">

        <id name="id" type="int" column="id">
                <generator class="native"/>
                </id>

public class Osoba implements Interface {

    private int id;
    private String imie;
    private String nazwisko;
    private String email;
    private String telefon;
    private String uczelnia;
    private String doswiadczenie;    
    private String skadSlyszal;
    private List zainteresowania = new ArrayList(); 

  public Osoba(){ // domyslny    
  }

  // konstruktor zeby mozna bylo sobie w jednej linijce dodawac osobe
   public Osoba(String imie1, String nazwisko1, String telefon1, String email1, String uczelnia1, String doswiadczenie1, String skadSlyszal1 ){
       this.imie = imie1;
       this.nazwisko = nazwisko1;
       this.email = email1;
       this.telefon = telefon1;
       this.uczelnia = uczelnia1;
       this.doswiadczenie = doswiadczenie1;
       this.skadSlyszal = skadSlyszal1;
       }

Native: This generation strategy is the default . 本机:此生成策略是默认策略。 It simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY , although it might be TABLE or SEQUENCE depending upon how the database is configured. 它只是选择主键生成策略,该策略是所讨论数据库的默认策略,通常是IDENTITY ,尽管它可能是TABLESEQUENCE具体取决于数据库的配置方式。 The native strategy is typically recommended, as it makes your code and your applications most portable. 通常建议使用本机策略,因为它会使您的代码和应用程​​序最易于移植。

For example: In Mysql if you have primary key column as auto_increment , the db will be updated using this strategy. 例如:在Mysql中,如果主键列为auto_increment ,则将使用此策略更新数据库。

Using annotations - 使用注释-

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="course_seq")
@SequenceGenerator(
    name="course_seq",
    sequenceName="course_sequence",
    allocationSize=1
)
private int id;

allocation size of 1 will increment it by 1. 分配大小1会将其增加1。

I had the same problem, but increment was 32 instead of 4 as for you: I solved it by closing sessionFactory . 我有同样的问题,但是增量是32,而不是您的4:我通过关闭sessionFactory解决了它。 I'm using H2 database, so here is link to documentation: http://www.h2database.com/html/grammar.html#set_cache_size 我正在使用H2数据库,因此这里是文档的链接: http : //www.h2database.com/html/grammar.html#set_cache_size

Where it says: 它说:

The cache is the number of pre-allocated numbers. 高速缓存是预分配编号的数量。 If the system crashes without closing the database, at most this many numbers are lost. 如果系统崩溃而没有关闭数据库,则最多将丢失这么多数字。 The default cache size is 32. To disable caching, use the cache size 1 or lower. 默认缓存大小为32。要禁用缓存,请使用缓存大小1或更小。

So when I didn't close sessionFactory in my Main method, the increment was 32, but when I started closing it, it worked. 因此,当我没有在Main方法中关闭sessionFactory时,增量为32,但是当我开始关闭它时,它起作用了。 I re-created database, and it worked. 我重新创建了数据库,它正常工作。 And when I looked to INFORMATION_SCHEMA.SEQUENCES table, which is the table with properties of sequence generation of my database , I saw that magic CACHE_SIZE = 32 . 当我查看INFORMATION_SCHEMA.SEQUENCES表时,该表是具有数据库序列生成属性的表,我看到了神奇的CACHE_SIZE = 32

So every time database was not closed properly, the sequence lost exactly 32 numbers. 因此,每次未正确关闭数据库时,该序列就会丢失32个数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM