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

How to automatically generate id from 1 with JPA?

nazar_art
1#
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:

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

    private String postText;    
    @ManyToOne
    private BlogUser user;    
    private LocalDateTime createdDate;
}

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

Saving objects looks like following:

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

    postService.addPost(firstPost);
    postService.addPost(secondPost);
    postService.addPost(thirdPost);

    BlogUser sailor = BlogUser.builder()
            .userName("sailor").password("123").email("sailor@gmail.com").build();
    userService.addUser(sailor);

    Comment commentToFirstPost = Comment.builder().commentText("you an idiot!")
            .user(sailor).post(firstPost).createdDate(LocalDateTime.now()).build();
    Comment secondCommentToFirstPost = Comment.builder().commentText("You should sail to Antarctica!")
            .user(sailor).post(firstPost).createdDate(LocalDateTime.now()).build();

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

UPDATE:

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

How to solve this issue?

Maciej Kowalski
2#
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
3#
Andres Reply to 2018-01-12 20:48:36Z

Do something like this:

public class Post 
{
    @Id
    @SequenceGenerator(name="seq",sequenceName="my_seq")        
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")               
    private Integer id;
}

Use a different sequence for each entity.

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

Use initialValue attribute of TableGenerator

  @Id
  @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