skip to Main Content

i am new in spring boot and such things, this is the first project i was trying to explore and was following a lecture for project creation, i made an entity class with @entity and @table annotations, when i executed the project, two table in mysql db got created namely blog and blog_seq, i want to understand how this blog_seq got created and what is the significance to this table.

entity class:
package com.SpringBoot.blog.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;

@Entity
@Table(name = "posts", uniqueConstraints = {@UniqueConstraint(columnNames = {"titles"})})
public class Posts {
        @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @Column(name = "titles", nullable = false)
    private String title;
    @Column(name = "description", nullable = false)
    private String description;
    @Column(name = "content", nullable = false)
    private String content;

    public Posts() {
        super();
    }

    public Posts(long id, String title, String description, String content) {
        super();
        this.id = id;
        this.title = title;
        this.description = description;
        this.content = content;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
 
}

application.properties:

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/blog
spring.datasource.userName=root
spring.datasource.password=123456789
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update

mysql workbench:

mysql workbench screenshot

thanks for the help!!

tried changing the entity annotations and application.properties somewhat, but not working

3

Answers


  1. It basically happened due to:

    @GeneratedValue(strategy=GenerationType.AUTO)
    

    AUTO strategy uses the database sequence to generate the unique value for your id column. If you log SQL queries then you would be able to see an extra query against that blog_seq to select the next unique value.

    If that is not what you expect then you can use:

    @GeneratedValue(strategy=GenerationType.IDENTITY)
    

    It will use the database auto-increment id column and let the database generate next value for your id column.

    It might not matter a lot for your project but do read about the performance implications of these two strategies.

    Edit:
    Found this article which explains these strategies nicely. It will help you.
    https://thorben-janssen.com/jpa-generate-primary-keys/

    Login or Signup to reply.
  2. Hibernate needs an unique identifier for each entity. It is the field annotated with @Id and corresponding column id in the database table posts. This field could be provided by the application while creating a new entity (inserting a new row) but it would be hard to maintain. Usually the id will be generated automatically by the database or hibernate. This can be configured with the annotation @GeneratedValue.

    In you case you have defined GenerationType.AUTO. This means that Hibernate can decide what kind of id generation to use. Depending on the database. Mostly it will choose a database sequence. But MySQL doesn’t support sequences. That’s why Hibernate will create a separate table to mimic a sequence. This is exactly what you can see as posts_seq table. It’s only column next_val holds the current value of the id for the table posts.

    Login or Signup to reply.
  3. A sequence is a user-defined schema-bound object that yields a sequence of integers based on a specified specification

    In simple words, it generates numbers in a sequence. This should give a better idea.

    In your case, posts_seq is the sequence generator associated with the posts table. It is used to generate id here. The value of next_value in posts_seq will be used to generate the id for the next row inserted to posts table. More info.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search