zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Luogu-P2295 MICE
    Luogu-P2627 修剪草坪
    Loj-10176-最大连续和
    Luogu-P1886 滑动窗口
    Luogu-P3807 【模板】卢卡斯定理
    Luogu-P1879 [USACO06NOV]玉米田Corn Fields
    Luogu-P1896 [SCOI2005]互不侵犯
    Loj-SGU 223-国王
    Luogu-P2657 [SCOI2009]windy数
    素数
  • 原文地址:https://www.cnblogs.com/vipsoft/p/12490033.html
Copyright © 2011-2022 走看看