简体   繁体   中英

Hibernate Reserved keyword escaping not working

I am getting below error due to the use of MYSQL reserved keywords in hibernate models.

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 

I have below property set in my application.properties file, it used to work before, but now it's not working and I am unable to figure out why.

spring.jpa.properties.hibernate.globally_quoted_identifiers=true

Now I have to escape the columns manually in my model definition for it to work.

@Column(name = "`who`")
private String who;

@Column(name = "`when`")
private Date when;

@Column(name = "`why`")
private String why;

How can I further debug for the same? How can I ensure that this property is being picked up?

We are using multiple DB connections in our spring boot project. So all the hibernate properties need to be configured in the 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM