Home How to automatically generate id from 1 with JPA?
Reply: 3

How to automatically generate id from 1 with JPA?

nazar_art Published in 2018-01-12 15:42:13Z

I am working on rest web services. I found some issue with autogenerated Id with JPA and Spring Boot.
Here are models:

public class Post {    
    @Id @GeneratedValue
    private Long id;

    private String postText;    
    private BlogUser user;    
    private LocalDateTime createdDate;

public class Comment {    
    @Id @GeneratedValue
    private Long id;    
    private String commentText;

Saving objects looks like following:

    Post firstPost = Post.builder()
            .postText("First post !!! UUUUUhuuuuu!")
    Post secondPost = Post.builder()
            .postText("I like this blog posting so much :)")
    Post thirdPost = Post.builder()
            .postText("To be or not to be? What is the question.")


    BlogUser sailor = BlogUser.builder()

    Comment commentToFirstPost = Comment.builder().commentText("you an idiot!")
    Comment secondCommentToFirstPost = Comment.builder().commentText("You should sail to Antarctica!")

However, after it I have instances in DB:

  • Posts:
    1 First post
    2 Second post
    3 Third post

  • Comments:
    4 First comment
    5 Second comment

I want to make comments iteration from 1 because it is completely another class. Not related to posts. It should be like following:

1 First comment
2 Second comment


DB is PostgreSQL. Also, I am interested to know how to do it for MySQL.

How to solve this issue?

Maciej Kowalski
Maciej Kowalski Reply to 2018-01-12 15:55:26Z

When you use the vanilla @GeneratedValue, its set-up with a javax.persistence.GenerationType.AUTO, which:

Indicates that the persistence provider should pick an appropriate strategy for the particular database.

In most cases that would be actually the GenerationType.SEQUENCE.

In that case hibernate would use its internal sequence for field annotated with the plain / vanilla style like yours.

That would explain that the counter does not restart for each of the entities as the same sequence is used there.

You could try forcing the native id generation though:

@GeneratedValue(strategy = GenerationType.IDENTITY)
Andres Reply to 2018-01-12 20:48:36Z

Do something like this:

public class Post 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")               
    private Integer id;

Use a different sequence for each entity.

pvpkiran Reply to 2018-01-12 15:55:46Z

Use initialValue attribute of TableGenerator

  @TableGenerator(name = "COMMENT_GEN",
                  table = "id_gen",
                  pkColumnName = "seq_name",
                  valueColumnName = "seq_number",
                  initialValue = 1)
  @GeneratedValue(strategy = GenerationType.TABLE, generator = "COMMENT_GEN")
  private Long id; 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.55347 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO