Primary key is not starting from 1 after truncating table data










0















I am using



@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;


in UserDetails table. But, once I truncate UserDetails table, it is not starting Id value from initial onwards. I didnt give SEQUENCE in db, but it is adding as sequence.










share|improve this question
























  • 1) what database are you using? 2) do you see a table hibernate_sequence in your database?

    – Simon Martinelli
    Nov 15 '18 at 7:27






  • 1





    AUTO says leave it to the JPA provider to do whatever it wants regarding value generation (so it uses a SEQUENCE in your case ... but it could equally have used AUTO_INCREMENT or whatever) and then you, without JPA knowing about it, truncate the table. So it continues to use the SEQUENCE from where it got to. Expected result is to not start from initial. If you want to go about TRUNCATing a table then you also need to remove/reset the SEQUENCE also

    – Billy Frost
    Nov 15 '18 at 7:27











  • Why this is a problem for you? The only requirement for auto-generated ID is uniqueness. You should not rely on any other property

    – Alex Salauyou
    Nov 15 '18 at 7:31
















0















I am using



@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;


in UserDetails table. But, once I truncate UserDetails table, it is not starting Id value from initial onwards. I didnt give SEQUENCE in db, but it is adding as sequence.










share|improve this question
























  • 1) what database are you using? 2) do you see a table hibernate_sequence in your database?

    – Simon Martinelli
    Nov 15 '18 at 7:27






  • 1





    AUTO says leave it to the JPA provider to do whatever it wants regarding value generation (so it uses a SEQUENCE in your case ... but it could equally have used AUTO_INCREMENT or whatever) and then you, without JPA knowing about it, truncate the table. So it continues to use the SEQUENCE from where it got to. Expected result is to not start from initial. If you want to go about TRUNCATing a table then you also need to remove/reset the SEQUENCE also

    – Billy Frost
    Nov 15 '18 at 7:27











  • Why this is a problem for you? The only requirement for auto-generated ID is uniqueness. You should not rely on any other property

    – Alex Salauyou
    Nov 15 '18 at 7:31














0












0








0








I am using



@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;


in UserDetails table. But, once I truncate UserDetails table, it is not starting Id value from initial onwards. I didnt give SEQUENCE in db, but it is adding as sequence.










share|improve this question
















I am using



@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;


in UserDetails table. But, once I truncate UserDetails table, it is not starting Id value from initial onwards. I didnt give SEQUENCE in db, but it is adding as sequence.







java jpa spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 7:57









secret super star

1,011115




1,011115










asked Nov 15 '18 at 7:11









user6044627user6044627

518




518












  • 1) what database are you using? 2) do you see a table hibernate_sequence in your database?

    – Simon Martinelli
    Nov 15 '18 at 7:27






  • 1





    AUTO says leave it to the JPA provider to do whatever it wants regarding value generation (so it uses a SEQUENCE in your case ... but it could equally have used AUTO_INCREMENT or whatever) and then you, without JPA knowing about it, truncate the table. So it continues to use the SEQUENCE from where it got to. Expected result is to not start from initial. If you want to go about TRUNCATing a table then you also need to remove/reset the SEQUENCE also

    – Billy Frost
    Nov 15 '18 at 7:27











  • Why this is a problem for you? The only requirement for auto-generated ID is uniqueness. You should not rely on any other property

    – Alex Salauyou
    Nov 15 '18 at 7:31


















  • 1) what database are you using? 2) do you see a table hibernate_sequence in your database?

    – Simon Martinelli
    Nov 15 '18 at 7:27






  • 1





    AUTO says leave it to the JPA provider to do whatever it wants regarding value generation (so it uses a SEQUENCE in your case ... but it could equally have used AUTO_INCREMENT or whatever) and then you, without JPA knowing about it, truncate the table. So it continues to use the SEQUENCE from where it got to. Expected result is to not start from initial. If you want to go about TRUNCATing a table then you also need to remove/reset the SEQUENCE also

    – Billy Frost
    Nov 15 '18 at 7:27











  • Why this is a problem for you? The only requirement for auto-generated ID is uniqueness. You should not rely on any other property

    – Alex Salauyou
    Nov 15 '18 at 7:31

















1) what database are you using? 2) do you see a table hibernate_sequence in your database?

– Simon Martinelli
Nov 15 '18 at 7:27





1) what database are you using? 2) do you see a table hibernate_sequence in your database?

– Simon Martinelli
Nov 15 '18 at 7:27




1




1





AUTO says leave it to the JPA provider to do whatever it wants regarding value generation (so it uses a SEQUENCE in your case ... but it could equally have used AUTO_INCREMENT or whatever) and then you, without JPA knowing about it, truncate the table. So it continues to use the SEQUENCE from where it got to. Expected result is to not start from initial. If you want to go about TRUNCATing a table then you also need to remove/reset the SEQUENCE also

– Billy Frost
Nov 15 '18 at 7:27





AUTO says leave it to the JPA provider to do whatever it wants regarding value generation (so it uses a SEQUENCE in your case ... but it could equally have used AUTO_INCREMENT or whatever) and then you, without JPA knowing about it, truncate the table. So it continues to use the SEQUENCE from where it got to. Expected result is to not start from initial. If you want to go about TRUNCATing a table then you also need to remove/reset the SEQUENCE also

– Billy Frost
Nov 15 '18 at 7:27













Why this is a problem for you? The only requirement for auto-generated ID is uniqueness. You should not rely on any other property

– Alex Salauyou
Nov 15 '18 at 7:31






Why this is a problem for you? The only requirement for auto-generated ID is uniqueness. You should not rely on any other property

– Alex Salauyou
Nov 15 '18 at 7:31













3 Answers
3






active

oldest

votes


















1














First, When you use @GeneratedValue(strategy = GenerationType.AUTO) annotation the persistence provider will determine values based on the type of the primary key attribute.



This type can be numerical or UUID. For numeric values (Your situation) the generation is based on a sequence or table generator. So the primary key values will be unique at the database level.



Now let's back to your question.
What you can do is that to finding the generated sequence on your database and re-create it.






share|improve this answer






























    0














    If you use AUTO, if you using hibernate, it will choose one of the strategies to generate your id. From the Reference:




    AUTO - either identity column, sequence or table depending on the
    underlying DB.







    share|improve this answer
































      0














      I added



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


      If I am truncating table, I will DROP sequence using, DROP SEQUENCE SEQ_DATA;
      Then I will create SEQ_DATA again.



      create sequence SEQ_DATA minvalue 1 maxvalue 999999999999999999999
      start with 1 increment by 1 cache 20;






      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314140%2fprimary-key-is-not-starting-from-1-after-truncating-table-data%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        First, When you use @GeneratedValue(strategy = GenerationType.AUTO) annotation the persistence provider will determine values based on the type of the primary key attribute.



        This type can be numerical or UUID. For numeric values (Your situation) the generation is based on a sequence or table generator. So the primary key values will be unique at the database level.



        Now let's back to your question.
        What you can do is that to finding the generated sequence on your database and re-create it.






        share|improve this answer



























          1














          First, When you use @GeneratedValue(strategy = GenerationType.AUTO) annotation the persistence provider will determine values based on the type of the primary key attribute.



          This type can be numerical or UUID. For numeric values (Your situation) the generation is based on a sequence or table generator. So the primary key values will be unique at the database level.



          Now let's back to your question.
          What you can do is that to finding the generated sequence on your database and re-create it.






          share|improve this answer

























            1












            1








            1







            First, When you use @GeneratedValue(strategy = GenerationType.AUTO) annotation the persistence provider will determine values based on the type of the primary key attribute.



            This type can be numerical or UUID. For numeric values (Your situation) the generation is based on a sequence or table generator. So the primary key values will be unique at the database level.



            Now let's back to your question.
            What you can do is that to finding the generated sequence on your database and re-create it.






            share|improve this answer













            First, When you use @GeneratedValue(strategy = GenerationType.AUTO) annotation the persistence provider will determine values based on the type of the primary key attribute.



            This type can be numerical or UUID. For numeric values (Your situation) the generation is based on a sequence or table generator. So the primary key values will be unique at the database level.



            Now let's back to your question.
            What you can do is that to finding the generated sequence on your database and re-create it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 7:27









            Mohammadreza KhatamiMohammadreza Khatami

            1,1051922




            1,1051922























                0














                If you use AUTO, if you using hibernate, it will choose one of the strategies to generate your id. From the Reference:




                AUTO - either identity column, sequence or table depending on the
                underlying DB.







                share|improve this answer





























                  0














                  If you use AUTO, if you using hibernate, it will choose one of the strategies to generate your id. From the Reference:




                  AUTO - either identity column, sequence or table depending on the
                  underlying DB.







                  share|improve this answer



























                    0












                    0








                    0







                    If you use AUTO, if you using hibernate, it will choose one of the strategies to generate your id. From the Reference:




                    AUTO - either identity column, sequence or table depending on the
                    underlying DB.







                    share|improve this answer















                    If you use AUTO, if you using hibernate, it will choose one of the strategies to generate your id. From the Reference:




                    AUTO - either identity column, sequence or table depending on the
                    underlying DB.








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 7:40

























                    answered Nov 15 '18 at 7:32









                    secret super starsecret super star

                    1,011115




                    1,011115





















                        0














                        I added



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


                        If I am truncating table, I will DROP sequence using, DROP SEQUENCE SEQ_DATA;
                        Then I will create SEQ_DATA again.



                        create sequence SEQ_DATA minvalue 1 maxvalue 999999999999999999999
                        start with 1 increment by 1 cache 20;






                        share|improve this answer



























                          0














                          I added



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


                          If I am truncating table, I will DROP sequence using, DROP SEQUENCE SEQ_DATA;
                          Then I will create SEQ_DATA again.



                          create sequence SEQ_DATA minvalue 1 maxvalue 999999999999999999999
                          start with 1 increment by 1 cache 20;






                          share|improve this answer

























                            0












                            0








                            0







                            I added



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


                            If I am truncating table, I will DROP sequence using, DROP SEQUENCE SEQ_DATA;
                            Then I will create SEQ_DATA again.



                            create sequence SEQ_DATA minvalue 1 maxvalue 999999999999999999999
                            start with 1 increment by 1 cache 20;






                            share|improve this answer













                            I added



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


                            If I am truncating table, I will DROP sequence using, DROP SEQUENCE SEQ_DATA;
                            Then I will create SEQ_DATA again.



                            create sequence SEQ_DATA minvalue 1 maxvalue 999999999999999999999
                            start with 1 increment by 1 cache 20;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 9:39









                            user6044627user6044627

                            518




                            518



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314140%2fprimary-key-is-not-starting-from-1-after-truncating-table-data%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                What does pagestruct do in Eviews?

                                Dutch intervention in Lombok and Karangasem

                                Channel Islands