I am working on other web services. I found some problems with automatically generated Id
with JPA and Spring Boot.
Here are the 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 is as follows:
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 that I have instances in the database:
I want to make comments iterate off 1
because this is a completely different class. Not related to messages. It should look like the following:
1 first comment
2 second comment
UPDATE:
PostgreSQL. , MySQL.
?