我遇到 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();
}
如果您需要更多信息,请告诉我。