0

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(){

    }
}
Mamun
  • 375
  • 2
  • 8
  • 17
  • You should read up on sequences as you don't seem to know what a sequence is. That will answer the question you never asked – XtremeBaumer Aug 09 '19 at 11:30
  • @XtremeBaumer, thanks for your reply. I read about sequence and identity, here is a good answer about this topic in Stackoverflow: https://stackoverflow.com/questions/8955074/generatedvaluestrategy-identity-vs-generatedvaluestrategy-sequence But I did not find my answer here. Probably I did not clear my question enough. I am editing my question. – Mamun Aug 09 '19 at 11:38
  • Show how your classes are mapped – XtremeBaumer Aug 09 '19 at 11:45
  • @XtremeBaumer, please check my edited question, I have added the class for which I want to recreate the table. – Mamun Aug 09 '19 at 11:50
  • Srsly, read up on sequences and how they work. https://www.postgresql.org/docs/9.5/sql-createsequence.html – XtremeBaumer Aug 09 '19 at 11:53
  • Check this post, it might be helpful. [How to reset hibernate sequence](https://stackoverflow.com/questions/43586360/how-to-reset-hibernate-sequence-generators). – YoManTaMero Aug 09 '19 at 12:29

0 Answers0