org.hibernate.HibernateException:当'hibernate.dialect'未设置时,对DialectResolutionInfo的访问不能为null [英] org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
问题描述
引起:org.hibernate.HibernateException:当'hibernate.dialect'没有在org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect中设置
时,DialectResolutionInfo的访问不能为空(DialectFactoryImpl.defaultDialect(DialectFactoryImpl.java:104 )美元,org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71 b $ b)
在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java :205)在org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111
)在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService
(AbstractServiceRegistryImpl.java:234 )$ or
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java :206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:850)
在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:843)
。在org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
。在org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
在org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerF在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory。)中,
$ or $。
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
... 21 more
我的pom.xml文件是这样的:
<亲本GT;
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-parent< / artifactId>
< version> 1.1.8.RELEASE< / version>
< / parent>
<依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-actuator< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-web< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-config< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-taglibs< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
< /依赖关系>
< dependency>
< groupId> commons-dbcp< / groupId>
< artifactId> commons-dbcp< / artifactId>
< /依赖关系>
< /依赖关系>
我的hibernate配置是这样的(方言配置在这个类的最后一个方法中) p>
@Configuration
@EnableTransactionManagement
@ComponentScan({com.spring.app})
public class HibernateConfig {
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String [] {com.spring.app.model});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
@Bean
public DataSource restDataSource(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(org.postgresql.Driver);
dataSource.setUrl(jdbc:postgresql:// localhost:5432 / teste?charSet = LATIN1);
dataSource.setUsername(klebermo);
dataSource.setPassword(123);
返回dataSource;
$Be
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
返回txManager;
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
属性hibernateProperties(){
返回新的属性(){
/ **
*
* /
private static final long serialVersionUID = 1L;
{
setProperty(hibernate.hbm2ddl.auto,create);
setProperty(hibernate.show_sql,false);
setProperty(hibernate.dialect,org.hibernate.dialect.PostgreSQLDialect);
}
};
}
}
我在这里做错了什么?
首先删除所有配置Spring Boot将为您启动它。如果你真的需要一个 SessionFactory
而不是 EntityManagerFactory
添加一个 HibernateJpaSessionFactoryBean
。
请确保您的类路径中包含 application.properties
,并添加以下属性。
spring.datasource.driverClassName = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql:// localhost:5432 / teste?charSet = LATIN1
spring.datasource.username = klebermo
spring.datasource.password = 123
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql = false
spring.jpa.hibernate.ddl-auto =创建
如果您确实需要访问 SessionFactory
,并且基本上用于相同的数据源,那么您可以执行以下操作href =http://docs.spring.io/spring-data/jpa/docs/current/reference/html/# =noreferrer>这里虽然是XML,而不是JavaConfig)。 p>
@Configuration
注释,因为这将由Spring Boot为您启用。
public class HibernateConfig {
@Bean
public HibernateJpaSessionF actoryBean sessionFactory(EntityManagerFactory emf){
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
退货工厂;
$ / code $ / pre
这样你就有一个
EntityManagerFactory
和一个SessionFactory
。
假设你有一个<使用
@EnableAutoConfiguration
的方法,您不需要@EnableTransactionManagement
main com.spring.app
包中的基本应用程序类应该足够了。@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String [] args)throws Exception {
SpringApplication.run(Application.class,args);
}
}
类似的东西应该足够检测到您的所有类(包括实体和基于Spring数据库)。
我还建议删除
commons-dbcp
依赖,因为这将允许Spring Boot配置更快,更强大的HikariCP
实现。I am trying run a spring-boot application which uses hibernate via spring-jpa, but i am getting this error:
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:205) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550) ... 21 more
my pom.xml file is this:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> </dependency> </dependencies>
my hibernate configuration is that (the dialect configuration is in the last method from this class):
@Configuration @EnableTransactionManagement @ComponentScan({ "com.spring.app" }) public class HibernateConfig { @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(restDataSource()); sessionFactory.setPackagesToScan(new String[] { "com.spring.app.model" }); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; } @Bean public DataSource restDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://localhost:5432/teste?charSet=LATIN1"); dataSource.setUsername("klebermo"); dataSource.setPassword("123"); return dataSource; } @Bean @Autowired public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) { HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setSessionFactory(sessionFactory); return txManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } Properties hibernateProperties() { return new Properties() { /** * */ private static final long serialVersionUID = 1L; { setProperty("hibernate.hbm2ddl.auto", "create"); setProperty("hibernate.show_sql", "false"); setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); } }; } }
what I am doing wrong here?
解决方案First remove all of your configuration Spring Boot will start it for you. If you really need a
SessionFactory
instead of anEntityManagerFactory
add aHibernateJpaSessionFactoryBean
.Make sure you have an
application.properties
in your classpath and add the following properties.spring.datasource.driverClassName=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=LATIN1 spring.datasource.username=klebermo spring.datasource.password=123 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=create
If you really need access to a
SessionFactory
and that is basically for the same datasource, then you can do the following (which is also docmented here although for XML, not JavaConfig).@Configuration public class HibernateConfig { @Bean public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) { HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean(); factory.setEntityManagerFactory(emf); return factory; } }
That way you have both an
EntityManagerFactory
and aSessionFactory
.Assuming you have a class with a
main
method with@EnableAutoConfiguration
you don't need the@EnableTransactionManagement
annotation, as that will be enabled by Spring Boot for you. A basic application class in thecom.spring.app
package should be enough.@Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
Something like that should be enough to have all your classes (including entities and Spring Data based repositories) detected.
I would also suggest removing the
commons-dbcp
dependency as that would allow Spring Boot to configure the faster and more robustHikariCP
implementation.这篇关于org.hibernate.HibernateException:当'hibernate.dialect'未设置时,对DialectResolutionInfo的访问不能为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!