Hibernate 说 table 不存在但确实存在
Hibernate says that table doesn't exist but it does
我遇到 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 和 [=38 添加了 not-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 的工作原理了解甚少。它加载了整套自动配置 classess,在我的例子中,它为 Hibernate 设置了不正确的命名策略。我所要做的就是排除 Hibernate 自动配置 class
@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})
突然之间,一切都如我所愿。
非常感谢@Nicolas 用他的评论为我指明了正确的方向。
让我吃惊的是 根据 Spring 引导参考:
Auto-configuration is noninvasive, at any point you can start to define your own configuration to replace specific parts of the auto-configuration. For example, if you add your own DataSource bean, the default embedded database support will back away.
但在我的情况下它不起作用。 并且它工作得很好,我只需要 "override" 更正 bean(正如@Oliver 所指出的)
我遇到 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 和 [=38 添加了 not-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 的工作原理了解甚少。它加载了整套自动配置 classess,在我的例子中,它为 Hibernate 设置了不正确的命名策略。我所要做的就是排除 Hibernate 自动配置 class
@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})
突然之间,一切都如我所愿。 非常感谢@Nicolas 用他的评论为我指明了正确的方向。
让我吃惊的是 根据 Spring 引导参考:
Auto-configuration is noninvasive, at any point you can start to define your own configuration to replace specific parts of the auto-configuration. For example, if you add your own DataSource bean, the default embedded database support will back away.
但在我的情况下它不起作用。 并且它工作得很好,我只需要 "override" 更正 bean(正如@Oliver 所指出的)