JPA 表名大小寫問題


JPA 默認會將實體中的 TABLE_NAME 轉成小寫如

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id
    private String id;

會報:java.sql.SQLSyntaxErrorException: Table 'mysql.employee' doesn't exist 表名已經被轉成了小寫

可以添加一個策略解決此問題

package com.iron.config;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

/**
 * Created by Jimmy on 2020/3/13.
 */
public class UpperTableStrategy extends PhysicalNamingStrategyStandardImpl {

    private static final long serialVersionUID = 1383021413247872469L;


    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        // 將表名全部轉換成大寫
        String tableName = name.getText().toUpperCase();

        return name.toIdentifier(tableName);
    }
}

application.yml 配置文件中添加相應的配置,啟用上面的策略

server:
  port: 8081
spring:
  jpa:
    show-sql: true
    hibernate:
      naming:
        physical-strategy: com.iron.config.UpperTableStrategy
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://172.17.127.53:3306/mysql?Unicode=true&characterEncoding=UTF-8
    username: root
    password: 123

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM