Spring Data Jpa Example Method for Automatically Generating Table Structure

  • 2021-07-26 07:32:43
  • OfStack

Want to initialize the data script as the application starts at deployment time, isn't this Spring Data Jpa Automatically generate the table structure in, which sounds particularly simple, is it not configuration Hibernate Adj. ddl-auto Well, everyone knows what to say. At the beginning, I thought so too. I actually operated one. Although the table was created successfully, the field comments, character sets and database engine were all wrong. I didn't expect to overturn in these details.

After all, the overturned car has to be lifted by itself, so record 1 here.

Note: Used in this article Spring Data Jpa Version 2.1. 4. RELEASE

Take MySQL as an example, let me give an example here:


import com.fasterxml.jackson.annotation.*;
import org.hibernate.annotations.*;
import org.springframework.data.annotation.*;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;

@Entity
@javax.persistence.Table(name = "basic_city")
@org.hibernate.annotations.Table(appliesTo = "basic_city", comment = " Urban basic information ")
public class CityDO {
  
  @Id
  @GenericGenerator(name = "idGenerator", strategy = "uuid")
  @GeneratedValue(generator = "idGenerator")
  @Column(name = "CITY_ID", length = 32)
  private String cityId;
 
  @Column(name = "CITY_NAME_CN", columnDefinition = "VARCHAR(255) NOT NULL COMMENT ' Name (Chinese) '")
  private String cityNameCN;
  
  @Column(name = "CITY_NAME_EN", columnDefinition = "VARCHAR(255) NOT NULL COMMENT ' Name (English) '")
  private String cityNameEN;

  @Column(name = "LONGITUDE", precision = 10, scale = 7)
  private BigDecimal longitude;

  @Column(name = "LATITUDE", precision = 10, scale = 7)
  private BigDecimal latitude;

  @Column(name = "ELEVATION", precision = 5)
  private Integer elevation;

  @Column(name = "CITY_DESCRIPTION", length = 500)
  private String cityDescription;
  
  //  Construction method and get/set Method omission 
}

@ javax. persistence. Table modifies the default ORM rule, and the attribute name sets the table name; @ org. hibernate. annotations. Table description when building a table, and attribute comment modifies the table description; @ Id primary key @ GenericGenerator sets the primary key policy, where UUID of Hibernate is used to generate the primary key; @ GeneratedValue sets the primary key value, and the attribute generator is associated with name of the primary key policy; @ Column modifies the default ORM rule; name sets the field name in the table. If the table field and entity class attribute are the same, the attribute can not be written; unique sets whether the field is only 1 in the table, and the default is false; Whether nullable can be blank, the default is true; insertable indicates whether this field responds to writing when insert is operated, and the default is true; updatable indicates whether this field responds to modification during update operation, and defaults to true; columnDefinition is a custom field, you can use this property to set the field annotation; table indicates that when mapping multiple tables, the fields in the table of the specified table, and the default value is the table name of the main table; length is the length and only works for fields of type varchar, with a default length of 255; precision indicates how many bits a 1 is; scale indicates how many bits the decimal part accounts for the total number of bits of precision. In this example, the two are used together to ensure the accuracy of latitude and longitude;

Next, we need to set up the data engine and character set. The network inherits MySQL5InnoDBDialect a lot, but this class has expired, so we use MySQL5Dialect instead:


package com.jason.config;

import org.hibernate.dialect.MySQL5Dialect;
import org.springframework.stereotype.Component;

@Component
public class MySQL5TableType extends MySQL5Dialect {

  @Override
  public String getTableTypeString() {
    return "ENGINE=InnoDB DEFAULT CHARSET=utf8";
  }
}

Then apply the MySQL5TableType defined above in the Spring Boot configuration file, using spring. jpa. properties. hibernate. dialect configuration:


spring:
 datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/techno?useUnicode=true&characterEncoding=utf8
  username: root
  password: root
 jpa:
  show-sql: true
  hibernate:
   ddl-auto: update
  database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  properties:
   hibernate:
    dialect: com.jason.config.MySQL5TableType

At this point, Sprign Data Jpa generation table is completed, and applications can be deployed through ApplicaitonRunner or CommandLineRunner interface 1 key, eliminating unnecessary operations such as initializing SQL. For simple use of these two interfaces, please refer to another article of mine.


Related articles: