Java org.hibernate.HibernateException:未设置“hibernate.dialect”时,对 DialectResolutionInfo 的访问不能为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26548505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
提问by Kleber Mota
I am trying run a spring-boot application which uses hibernate via spring-jpa, but i am getting this error:
我正在尝试运行一个通过 spring-jpa 使用休眠的 spring-boot 应用程序,但我收到此错误:
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.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.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:
我的 pom.xml 文件是这样的:
<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?
我在这里做错了什么?
采纳答案by M. Deinum
First remove all of your configuration Spring Boot will start it for you. If you really need a SessionFactory
instead of an EntityManagerFactory
add a HibernateJpaSessionFactoryBean
.
首先删除你所有的配置 Spring Boot 会为你启动它。如果你真的需要一个SessionFactory
而不是一个EntityManagerFactory
添加一个HibernateJpaSessionFactoryBean
.
Make sure you have an application.properties
in your classpath and add the following properties.
确保您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=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 herealthough for XML, not JavaConfig).
如果您确实需要访问 aSessionFactory
并且基本上是针对相同的数据源,那么您可以执行以下操作(尽管对于 XML,而不是 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 a SessionFactory
.
这样你就有了 anEntityManagerFactory
和 a SessionFactory
。
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 the com.spring.app
package should be enough.
假设您有一个带有main
方法的类,@EnableAutoConfiguration
您不需要@EnableTransactionManagement
注释,因为 Spring Boot 将为您启用该注释。包中一个基本的应用程序类com.spring.app
应该就足够了。
@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.
这样的事情应该足以检测到您的所有类(包括实体和基于 Spring Data 的存储库)。
I would also suggest removing the commons-dbcp
dependency as that would allow Spring Boot to configure the faster and more robust HikariCP
implementation.
我还建议删除commons-dbcp
依赖项,因为这将允许 Spring Boot 配置更快、更健壮的HikariCP
实现。
回答by mhnagaoka
I was facing a similar problem when starting up the application (using Spring Boot) with the database server down.
在数据库服务器关闭的情况下启动应用程序(使用 Spring Boot)时,我遇到了类似的问题。
Hibernate can determine the correct dialect to use automatically, but in order to do this, it needs a live connection to the database.
Hibernate 可以自动确定要使用的正确方言,但为了做到这一点,它需要实时连接到数据库。
回答by ACV
I got this error when my database was not created. After creating the DB manually, it worked fine.
未创建数据库时出现此错误。手动创建数据库后,它工作正常。
回答by Eric B.
I ran into the same problem and my issue was that the DB I was trying to connect to didn't exist.
我遇到了同样的问题,我的问题是我试图连接的数据库不存在。
I created the DB, verified the URL/connection string and reran and everything worked as expected.
我创建了数据库,验证了 URL/连接字符串并重新运行,一切都按预期工作。
回答by Pankaj
I also faced a similar issue. But, it was due to the invalid password provided. Also, I would like to say your code seems to be old-style code using spring. You already mentioned that you are using spring boot, which means most of the things will be auto configured for you. hibernate dialect will be auto selected based on the DB driver available on the classpath along with valid credentials which can be used to test the connection properly. If there is any issue with the connection you will again face the same error. only 3 properties needed in application.properties
我也遇到了类似的问题。但是,这是由于提供的密码无效。另外,我想说您的代码似乎是使用 spring 的旧式代码。您已经提到您正在使用 spring boot,这意味着大部分内容都会为您自动配置。休眠方言将根据类路径上可用的数据库驱动程序以及可用于正确测试连接的有效凭据自动选择。如果连接有任何问题,您将再次面临相同的错误。application.properties 中只需要 3 个属性
# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3306/pdb1
# Replace with your credentials
spring.datasource.username=root
spring.datasource.password=
回答by Vikram S
Make sure your application.properties
has all correct info: (I changed my db port from 8889
to 3306
it worked)
确保您application.properties
拥有所有正确的信息:(我将数据库端口从8889
更改3306
为有效)
db.url: jdbc:mysql://localhost:3306/test
回答by zbig
In my case the user could not connect to the database. If will have same issue if the log contains a warning just before the exception:
在我的情况下,用户无法连接到数据库。如果日志在异常之前包含警告,则如果将有相同的问题:
WARN HHH000342: Could not obtain connection to query metadata : Login failed for user 'my_user'.
回答by user7169604
this is happening because your code is not bale to connect the database. Make sure you have mysql driver and username, password correct.
发生这种情况是因为您的代码无法连接数据库。确保你有 mysql 驱动程序和用户名,密码正确。
回答by obesechicken13
Make sure you have your database in your pom like OP did. That was my problem.
确保像 OP 一样在你的 pom 中有你的数据库。那是我的问题。
回答by doesnt_matter
My problem was that embedded database was already connected. close connection
我的问题是嵌入式数据库已经连接。紧密联系