前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
项目中使用 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