java - 创建名为“entityManagerFactory”的 bean 时出错。可能是什么问题?
问题描述
我尝试在没有 SpringBoot 的情况下创建 Spring data jpa 程序。配置类:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"springdata.repository"})
@ComponentScan(basePackages = {"springdata.entities.service"})
@PropertySource("classpath:data.properties")
public class SpringDataConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
final BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(env.getProperty("jdbc.url"));
basicDataSource.setUsername(env.getProperty("jdbc.user"));
basicDataSource.setPassword(env.getProperty("jdbc.password"));
return basicDataSource;
}
@Bean
public Properties hibernateProperties() {
final Properties hibernateProp = new Properties();
hibernateProp.put("hibernate.dialect", "org.hibernate.H2Dialect");
hibernateProp.put("hibernate.format_sql", true);
hibernateProp.put("hibernate.use_sql_comments", true);
hibernateProp.put("hibernate.show_sql", true);
hibernateProp.put("hibernate.hbm2ddl.auto", "update");
hibernateProp.put("hibernate.max_fetch_depth", 3);
hibernateProp.put("hibernate.jdbc.batch_size", 10);
hibernateProp.put("hibernate.jdbc.fetch_size", 50);
return hibernateProp;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final JpaVendorAdapter jpaVendorAdapter,
final Properties hibernateProperties,
final DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPackagesToScan("springdata.entities");
em.setDataSource(dataSource);
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaProperties(hibernateProperties);
em.afterPropertiesSet();
return em;
}
}
测试类:
public class Test {
public static void main(final String[] args) throws SQLException {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringDataConfig.class);
DuckService duckService = ctx.getBean(DuckService.class);
System.out.println(duckService.findAll().size());
}
}
maven依赖:
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.1.0.Alpha2</version>
</dependency>
</dependencies>
还有例外:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in springdata.SpringDataConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
我试图找到这个问题的决定,并且在 SO 上有类似的问题,解决方案是在 hibernate.javax.persistance 上替换 javax.persistance。但就我而言,我有新的例外。
解决方案
改变hibernate
方言
hibernateProp.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
推荐阅读
- machine-learning - Keras Switch operation
- ruby - 数组说明中的块/过程返回数组
- android-studio - 无法解析符号“GoogleApiClient”
- c# - C# Selenium - Explicitly waiting By.CssSelector(...)
- javascript - Load clicked URLs from AJAX loaded page
- android - 屏幕关闭时出现 DialogFragment IllegalStateException
- regex - Got error 'invalid UTF-8 string at offset 1' from regexp
- r - 在 R 中将绘图导出为 pdf:将图例添加到第二页但不是第一页
- git - .gitattributes 未显示正确的语言(语言学家可检测)
- javascript - 提交后渲染列表时如何解决转换组的问题