java - 休眠保留关键字转义不起作用
问题描述
MYSQL
由于在休眠模型中使用保留关键字,我遇到了错误。
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'when, who, why) values ('ICL670A - Project Deliverable - HAQ 1 - FDA-IR', null, ' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1109) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at
我在我的application.properties
文件中设置了以下属性,它以前可以工作,但现在它不起作用,我无法弄清楚为什么。
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
现在我必须在我的模型定义中手动转义列才能使其工作。
@Column(name = "`who`")
private String who;
@Column(name = "`when`")
private Date when;
@Column(name = "`why`")
private String why;
我该如何进一步调试?我怎样才能确保该物业正在被取走?
解决方案
我们在 Spring Boot 项目中使用了多个 DB 连接。所以所有的hibernate属性都需要在DataSourceConfigurationFile
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean idotdbEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(idotdbDataSource());
em.setPackagesToScan("com.novartis.idot.model");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
properties.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("spring.hibernate.dialect"));
properties.put("hibernate.globally_quoted_identifiers", env.getProperty("spring.jpa.properties.hibernate.globally_quoted_identifiers"));
properties.put("hibernate.id.new_generator_mappings", env.getProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSourceProperties idotdbDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource idotdbDataSource() {
return idotdbDataSourceProperties().initializeDataSourceBuilder().build();
}
@Primary
@Bean
public PlatformTransactionManager idotdbTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(idotdbEntityManager().getObject());
return transactionManager;
}
推荐阅读
- amazon-web-services - RPM 仅安装在 AWS ec2 redhat 实例上
- ios - 如何在 Swift 中设置 MediaPlayer 播放速率?
- c++ - 打印出用户从未输入的数字的数组副本
- spring - Spring 的 @RefreshScope 是坏了还是我用错了?
- python-2.7 - 尝试在数组中设置值
- javascript - Firebase 云函数“函数返回未定义、预期的承诺或值”
- r - 如何使用 R 中的非标准多项式函数调整非线性曲线拟合的系数
- java - Ganib (Java) - 无法发送验证邮件
- visual-studio-code - 在 Windows 中使用非默认终端
- c++ - 正在使用空指针调用静态成员 UB