0

I am stuck on strange issue with Spring + Hibernate.

I have some entity:

package arthur.khusnutdinov.mysitev2.pub.mods.db;
// Generated 05.09.2017 23:23:19 by Hibernate Tools 5.2.5.Final

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * SitePages generated by hbm2java
 */
@Entity
@Table(name = "site_pages", catalog = "artfunpw")
public class SitePages implements java.io.Serializable {

    private Integer id;
    private String pageUrl;
    private String pageHtmlContent;

And session configuration code:

@Bean
    public LocalSessionFactoryBean sessionFactory() {

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(dbDataSource);

        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        props.setProperty("hibernate.format_sql", "true");
        props.setProperty("hibernate.show_sql", "true");

        props.setProperty("net.sf.ehcache.configurationResourceName", "/ehcache.xml");
        props.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
        props.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
        props.setProperty("hibernate.cache.use_second_level_cache", "true");
        props.setProperty("hibernate.cache.use_query_cache", "true");
        factoryBean.setHibernateProperties(props);
        factoryBean.setPackagesToScan("arthur.khusnutdinov");

        return factoryBean;
    }

Everything works fine, untill table name in MySQL database is sitepages, but it doesn't work, if table name is site_pages - I've got java.sql.SQLSyntaxErrorException: Table 'artfunpw.sitepages' doesn't exist . It seems, like Hibernate is ignoring @Table(name = "site_pages", catalog = "artfunpw").

What am I doing wrong, how to make Hibernate to respect @Table annotation?

Thank you very much!

Update 1: Tried to add

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

into the application.properties - didn't helped.

hibernate.implicit_naming_strategy is not solved issue too.

Update 2: I use hibernate 5 :).

Arthur
  • 3,253
  • 7
  • 43
  • 75

1 Answers1

0

Solved!

The problem was in changes in Hibernate 5. I found a very useful answer here - ImprovedNamingStrategy no longer working in Hibernate 5 .

After I found that answer, I've changed my sessionFactory() - I added factoryBean.setPhysicalNamingStrategy(new PhysicalNamingStrategyStandardImpl()); - code:

@Bean
public LocalSessionFactoryBean sessionFactory() {

    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    factoryBean.setDataSource(dbDataSource);

    Properties props = new Properties();
    props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    props.setProperty("hibernate.format_sql", "true");
    props.setProperty("hibernate.show_sql", "true");

    props.setProperty("net.sf.ehcache.configurationResourceName", "/ehcache.xml");
    props.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
    props.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
    props.setProperty("hibernate.cache.use_second_level_cache", "true");
    props.setProperty("hibernate.cache.use_query_cache", "true");
    factoryBean.setHibernateProperties(props);

    factoryBean.setPhysicalNamingStrategy(new PhysicalNamingStrategyStandardImpl());

    return factoryBean;
}

I also changed code of the select statement. Now it looks like

List<SitePages> filmList = new ArrayList<>(); 

filmList = sessionFactory.getCurrentSession()
        .createNativeQuery("select * from site_pages")
        .setCacheable(false)
        .list();

System.out.println(filmList.size());

After these changes everything works fine :).

Arthur
  • 3,253
  • 7
  • 43
  • 75