gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:27:18 25 4
gpt4 key购买 nike

我遇到了 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;

以及 session 配置代码:

@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 :)。

最佳答案

解决了!

问题出在 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());

经过这些更改后,一切正常:)。

关于mysql - Spring + Hibernate - @Table 名称注释的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com