I am working with PostgreSQL database for a Spring RESTful API. I am using Spring Data, Spring JPA and Hibernate.
To produce a customized primary key, I am using SequenceGenerator. Everything works fine. The problem occurs when I am deleting a table from database and want create it again where the primary key will be again start from 1. I am deleting a table from database for some reason which will be created again. I am using pgAdmin UI where I am just dropping the table. Next time when I am creating the table again, Hibernate still remembers the last produced primary keys and thus it start producing primary keys from the next number of the last primary key, thus producing a totally different not 1 which I want. I think the reason is, hibernate is storing the record of last produced primary keys though the table has been deleted.
That's why I am looking for help how to force hibernate to start producing primary from from 1 (one) for a newly created table.
In property file, I have the following property:
spring.jpa.hibernate.ddl-auto=update
I tried to by setting the following,
spring.jpa.hibernate.ddl-auto=create
It solved the problem for this table but rest all of tables were also recreated, thus I lost all my data.
This are class for which I am trying to recreate a table.
@Data
@Entity
@Table(name = "professions")
public class Profession extends BasicEntity {
@Id
@Column(name = "profession_id", unique = true)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "profession_key_generator")
@SequenceGenerator(name = "profession_key_generator", allocationSize = 1)
private int profession_id;
@Column(name = "profession_rus_title", unique = true)
private String rusTitle;
@Column(name = "profession_eng_title", unique = true)
private String engTitle;
@Override
public Integer getId() {
return profession_id;
}
public Profession(String rusTitle, String engTitle){
this.rusTitle = rusTitle;
this.engTitle = engTitle;
}
public Profession(){
}
}