Primary key is not starting from 1 after truncating table data
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
add a comment |
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
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
add a comment |
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
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
java jpa spring-data-jpa
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 '18 at 7:27
Mohammadreza KhatamiMohammadreza Khatami
1,1051922
1,1051922
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 15 '18 at 7:40
answered Nov 15 '18 at 7:32
secret super starsecret super star
1,011115
1,011115
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 15 '18 at 9:39
user6044627user6044627
518
518
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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