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

package jp.co.confrage;

import java.io.Serializable;

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;

@Data
@Entity
@Table(name="employee_masters2")
public class Employee {
  @EmbeddedId
  private PrimaryK id;

  @Column(name="empname")
  private String empname;

  @Embeddable
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  public static class PrimaryK implements Serializable{
    private static final long serialVersionUID = -2523459362991270288L;
    @Column(name="id")
    private String id;

    @Column(name="empno")
    private String empno;
  }
}

Add the following to application.properties

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sampledb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=system
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

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 spring.jpa.show-sql=true 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.

application.yml.

spring:
  jpa:
    database: POSTGRESQL
    hibernate:
      ddl-auto: create
    show-sql: true
  datasource:
    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.

package jp.co.confrage.domain.entity;

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

import lombok.Data;

@Data
@Entity
@Table(
  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.

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL