解决 springboot + JPA + MySQL 表名全大写 出现 “表不存在” 问题(Table ‘XXX.xxx‘ doesn‘t exist)
前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。?
項目中使用 JPA 和 mysql 。表名是全大寫的。
出現 如下報錯:
java.sql.SQLSyntaxErrorException: Table 'XXX_ms.work_task' doesn't exist各種查詢后得知問題出在 hibernate 對于數據庫命名策略的配置上。
我目前的使用的應該是默認配置,會自動把表名從大寫轉換為小寫。
spring data jpa 是基于hibernate5.0 , 而?Hibernate5 關于數據庫命名策略的配置與之前版本略有不同:
不再支持早期的 hibernate.ejb.naming_strategy,而是直接替換為兩個新屬性:
hibernate.physical_naming_strategy
hibernate.implicit_naming_strategy
至于 physical_naming_strategy 則有兩個常用的配置:
org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl解決方法一:
可以在 springboot 項目中配置文件內加上配置行,設置命名為?無修改命名策略:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl解決方法二:
1)重寫命名策略中改表名為小寫的方法:
?
import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;/*** 重寫 hibernate 對于命名策略中改表名大寫為小寫的方法*/ public class MySQLUpperCaseStrategy extends PhysicalNamingStrategyStandardImpl {@Overridepublic Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {String tableName = name.getText().toUpperCase();return name.toIdentifier(tableName);}}2)在對應配置文件中 使用自己實現的策略
spring.jpa.hibernate.naming.physical-strategy=com.xxx.xxx.util.MySQLUpperCaseStrategy?
參考:
https://blog.csdn.net/q979076061/article/details/51539960
https://blog.csdn.net/jiangyu1013/article/details/80395579
http://blog.51cto.com/4528195/1983780
https://blog.csdn.net/holdlg/article/details/52252471
?
總結
以上是生活随笔為你收集整理的解决 springboot + JPA + MySQL 表名全大写 出现 “表不存在” 问题(Table ‘XXX.xxx‘ doesn‘t exist)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 判断一个数组中的值是否在另一个数组中
- 下一篇: C# 类构造函数赋值里属性与字段赋值注意