Search code examples
javamysqlhibernateauto-incrementidentity

Java Hibernate AutoIncrement Identity set ID to start 0 mySQL


i have a simple class like this

public class IdClass
{
    private static final long serialVersionUID = 4594459221202802623L;
    private Integer id;
    public IdClass(){}
    public IdClass(Integer id){super();this.id = id;}   
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId(){return this.id;}
    public void setId(Integer id){this.id = id;}        
}

which have autoIncrement in MYSQL table everything was working smoothly but we need to delete all records in the table to start once again later i save the new first record into de table but the sequence use the last ID generate (+1) before the delete i was wondering if it is possible to set the ID to start from 0 or 1 again...thanks a lot..


Solution

  • You can do it in three ways

    1. Directly Reset Autoincrement Value

      ALTER TABLE table_name AUTO_INCREMENT=1;

    2. Truncate Table

      TRUNCATE TABLE table_name;

      This will reset the auto increment on the table as well as deleting all records from that table.

    3. Drop and Recreate

      Table DROP TABLE table_name;

      CREATE TABLE table_name { ... };

    Source:

    How To Reset MySQL Autoincrement Column