问题描述
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table 'Library.book'不存在
我的依赖设置看起来像这样(我相信这可能是原因):
pre $ 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添加了not-spring-boot依赖关系(在不同的项目中使用了完全相同的依赖关系集,但没有spring-boot-starter-web并且一切正常) 。
在阅读其他人的问题后,我检查了我的@EnableJpaRepositories是否有正确的存储路径,以及entityManagerFactoryBean是否正确设置了packagesToScan。
我相信Spring Boot与其他依赖冲突,因为我的配置看起来很好。
现在我将显示一些代码片断,因为我可能是错误的我的配置的正确性; p
Book MySQL DDL:
CREATE TABLE如果不存在`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;
私有字符串标题;
私有字符串描述;
私人字符串流派;
@Temporal(TemporalType.DATE)
private发布日期;
$ b 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 = new Properties();
properties.setProperty(hibernate.generate_statistics,false);
properties.setProperty(hibernate.show_sql,false);
factory.setJpaProperties(properties);
factory.afterPropertiesSet();
返回factory.getObject();
}
请告诉我您是否需要更多信息。
解决方案问题在于我对Spring Boot的工作原理不甚了解。它加载了一整套自动配置类,在我的情况下,为Hibernate设置不正确的命名策略。我所要做的就是排除Hibernate的自动配置类。
$ b $ pre $ @ $ c $ @SpringBootApplication(exclude = {HibernateJpaAutoConfiguration.class})
突然间一切正常,就像我想要的那样。
非常感谢@Nicolas指点我的正确方向和他的评论。
根据到Spring Boot参考:
它工作得很好,我只需要覆盖正确的bean(正如@Oliver所指出的那样)。
I am experiencing problem with Hibernate throwing following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'Library.book' doesn't exist
My dependency setup looks like this (I believe this might be the reason):
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'
So I am using spring-boot-starter-web (project created using Spring CLI), then added not-spring-boot dependencies for Hibernate and Spring Data among other things (used the exact same dependency set in different projects, but without spring-boot-starter-web and everything worked just fine).
After reading others' questions I checked whether my @EnableJpaRepositories has correct path to Repositories and if entityManagerFactoryBean has packagesToScan set correctly.
I believe that Spring Boot has conflicts with other dependencies, because my configuration looks fine.
I will now show some code snippets, because I might be wrong about my configuration's correctness ;p
Book 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`)
)
Book entity:
@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();
}
Please tell me if you need more information.
The problem was my poor understanding of how Spring Boot works. It loads whole set of autoconfiguration classess, which in my case where setting incorrect naming strategy for Hibernate. All I had to do was exclude the Hibernate autoconfiguration class
@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})
and suddenly everything worked just like I wanted it to.Big thanks to @Nicolas for pointing me in the right directions with his comments.
according to Spring Boot Reference:
and it works perfectly fine, I just needed to "override" correct bean (as pointed out by @Oliver)
这篇关于Hibernate表示该表不存在,但它确实存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!