Spring + Hibernate - @Table名称注释问题

时间:2017-09-05 21:04:08

标签: mysql spring hibernate

我遇到了Spring + Hibernate的奇怪问题。

我有一些实体:

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;

会话配置代码:

@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;
    }

一切正常,直到MySQL数据库中的表名是 sitepages ,但是如果表名是 site_pages 则它不起作用 - 我已经得到java.sql.SQLSyntaxErrorException:表' artfunpw.sitepages'不存在。 似乎,就像Hibernate忽略了@Table(name = "site_pages", catalog = "artfunpw")

我做错了什么,如何让Hibernate尊重@Table注释?

非常感谢!

更新1 : 试图添加

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

进入 application.properties - 没有帮助。

hibernate.implicit_naming_strategy也没有解决问题。

更新2 :我使用hibernate 5:)。

1 个答案:

答案 0 :(得分:0)

解决!

问题在于Hibernate 5的变化。我在这里找到了一个非常有用的答案 - ImprovedNamingStrategy no longer working in Hibernate 5

在我找到答案后,我更改了sessionFactory() - 我添加了factoryBean.setPhysicalNamingStrategy(new PhysicalNamingStrategyStandardImpl()); - 代码:

@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;
}

我还更改了select语句的代码。现在看起来像

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

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

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

在这些改变之后一切正常:)。