How to automatically generate tables from entities in Spring Boot’s JPA

How to automatically generate tables from entities in Spring Boot’s JPA

I have implemented creating a table from a JPA entity.

First, let’s create an entity with the following composite primary key



import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class Employee {
  private PrimaryK id;

  private String empname;

  public static class PrimaryK implements Serializable{
    private static final long serialVersionUID = -2523459362991270288L;
    private String id;

    private String empno;

Add the following to


The database name is sampledb, using MySQL57Dialect, but the following will also work.

  • org.hibernate.dialect.MySQL55Dialect
  • org.hibernate.dialect.MySQL57Dialect ← This time we use this one (storage engine will be InnoDB)

For MariaDB, it must be either

  • org.hibernate.dialect.MariaDBDialect
  • org.hibernate.dialect.MariaDB53Dialect

Specifying spring.jpa.hibernate.ddl-auto=create will create the tables when the Spring Boot application starts.

Specifying will log to the console.

Hibernate: drop table if exists employee
Hibernate: create table employee(empno varchar(255) not null, id varchar(255) not null, empname varchar(255), primary key (empno, id)) engine=InnoDB

I added useUnicode=yes&characterEncoding=UTF-8 because the table character code is Latin1, but it is still Latin1, so I will change the following settings in my.ini.

  • default-character-set=utf-8
  • character-set-server=utf8

But restarting MySQL didn’t work.

create database DB名;

Check the character code.

mysql> show variables like 'character%';
| Variable_name            | Value                                                   |
| character_set_client     | utf8                                                    |
| character_set_connection | utf8                                                    |
| character_set_database   | utf8                                                    |
| character_set_filesystem | binary                                                  |
| character_set_results    | utf8                                                    |
| character_set_server     | utf8                                                    |
| character_set_system     | utf8                                                    |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |

Check with PostgreSQL

Check with PostgreSQL.


    database: POSTGRESQL
      ddl-auto: create
    show-sql: true
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/testdb
    username: postgres
    password: postgres

Creates an entity corresponding to a table.

This entity class is created by specifying the table name with the @Entity annotation and the unique index with the @Index annotation.

If a column name is omitted with the @Column annotation, the variable name becomes the column name.


import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;

import lombok.Data;

  name = "employee",
  indexes = @Index(name = "employee_index", columnList = "name,age", unique = true))
public class Employee {
  @Id private Long id;
  private String name;
  private Integer age;

Console log.

Hibernate: create table employee (id int8 not null, age int4, name varchar(255), primary key (id))
Hibernate: alter table if exists employee add constraint employee_index unique (name, age)

If you want to add a Not Null constraint, add @Column(nullable = false) to the field to add a Not Null constraint.