Hibernate 说该表不存在,但它确实存在

2024-03-05

我遇到 Hibernate 抛出以下错误的问题:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'Library.book' doesn't exist

我的依赖设置如下所示(我相信这可能是原因):

compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test") 

compile 'org.springframework:spring-orm:4.1.6.RELEASE'
compile 'org.springframework.data:spring-data-jpa:1.8.0.RELEASE'

compile 'org.hibernate:hibernate-entitymanager:4.3.8.Final'
compile 'org.hibernate:hibernate-core:4.3.8.Final'

compile 'org.apache.commons:commons-dbcp2:2.1'

compile 'mysql:mysql-connector-java:5.1.35'

compile 'org.apache.logging.log4j:log4j-core:2.2'

因此,我使用 spring-boot-starter-web (使用 Spring CLI 创建的项目),然后为 Hibernate 和 Spring Data 添加非 spring-boot 依赖项(在不同项目中使用完全相同的依赖项集,但没有 spring- boot-starter-web 一切正常)。

在阅读其他人的问题后,我检查了我的 @EnableJpaRepositories 是否具有正确的存储库路径以及entityManagerFactoryBean 是否正确设置了packagesToScan。

我相信 Spring Boot 与其他依赖项有冲突,因为我的配置看起来不错。

我现在将显示一些代码片段,因为我对配置的正确性可能是错误的;p

预订MySQL DDL:

CREATE TABLE IF NOT EXISTS `Library`.`Book` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(100) NOT NULL,
  `description` VARCHAR(256),
  `genre` VARCHAR(50) NOT NULL,
  `releaseDate` DATE NOT NULL,
  PRIMARY KEY (`id`)
)

图书实体:

@Entity
@Table(name="Book")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String title;
    private String description;
    private String genre;
    @Temporal(TemporalType.DATE)
    private Date releaseDate;
}

EntityManagerFactory bean:

@Bean
@Autowired(required = true)
public EntityManagerFactory entityManagerFactory(DataSource dataSource) {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setShowSql(false);
    vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
    vendorAdapter.setDatabase(Database.MYSQL);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("pl.com.imralav.library.data.entity");
    factory.setDataSource(dataSource);

    Properties properties = new Properties();
    properties.setProperty("hibernate.generate_statistics", "false");
    properties.setProperty("hibernate.show_sql", "false");

    factory.setJpaProperties(properties);

    factory.afterPropertiesSet();

    return factory.getObject();
}

如果您需要更多信息,请告诉我。


问题是我对 Spring Boot 的工作原理了解甚少。它加载整套自动配置类,在我的例子中,为 Hibernate 设置了错误的命名策略。我所要做的就是排除 Hibernate 自动配置类

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})

突然间,一切都按照我的意愿进行了。 非常感谢@Nicolas 他的评论为我指明了正确的方向。

One thing that surprises me was that according to Spring Boot Reference:

自动配置是非侵入性的,您可以随时开始定义自己的配置来替换自动配置的特定部分。例如,如果您添加自己的 DataSource bean,则默认的嵌入式数据库支持将消失。

but in my case it didn't work. and it works perfectly fine, I just needed to "override" correct bean (as pointed out by @Oliver)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hibernate 说该表不存在,但它确实存在 的相关文章

随机推荐