未设置’hibernate.dialect’时,连接不能为空

使用spring3设置hibernate有更多问题。 这次是说连接是nul,因为没有设置它在我的hibernate.cfg.xml文件中的方言。

这是完整的例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in URL [file:war/WEB-INF/datasource-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44) at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97) at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:172) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71) at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775) at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184) at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) ... 29 more 

这是我的dataSource-config.xml,它会使sessionfactory成为可能

                  

以下是hibernate.cfg.xml文件

       com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/freedom root password  25  org.hibernate.dialect.MySQLDialect  thread  org.hibernate.cache.NoCacheProvider  true true  create     

如你所见,方言正在设定中。

编辑:我的database.properties文件

  # DB properties file database.url=jdbc:mysql://localhost:3306/freedom database.driver=com.mysql.jdbc.Driver database.user=root database.password=password database.maxConnections=25 

编辑:这是一个完整的堆栈跟踪。 访问数据库可能是问题,但我可以通过命令提示符成功访问它?

  2288 [main] WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES)) 2289 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4f549ceb: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.config.viewControllerHandlerMapping,userService,myDataSource,mySessionFactory,transactionManager,propertyConfigurer,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,viewResolver,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,hello,userController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 2289 [main] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter - Invoking destroy method 'close' on bean with name 'myDataSource' 2289 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#14': [org.springframework.web.servlet.config.viewControllerHandlerMapping] 2289 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#8': [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0] 2289 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#1': [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0] 2290 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)': [org.springframework.web.servlet.handler.MappedInterceptor#0] 

数据库连接丢失。 将hibernate.cfg.xml文件添加到这样的行中

 com.mysql.jdbc.Driver  jdbc:mysql://localhost:3306/your_database  your_user your_password 

(如果您的计算机上未安装数据库,请替换localhost,并在我的示例中设置以your_开头的值)。

我有同样的问题。 从Apache Commons DataSource切换:

       

致C3P0:

       

让我的问题消失了。 也许有人可以解释为什么,我很高兴它的工作原理,并希望分享我所知道的一点点:)

我认为它的问题是不在您的应用程序中加载您的hibernate.cfg.xml文件。

我也遇到了同样的问题。 我在我的项目中尝试使用以下代码:

 Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); 

然后它运作良好。 希望这会有所帮助。

我正在使用hibernate和InnoDB为什么不将hibernate.dialect设置为org.hibernate.dialect.MySQL5org.hibernate.dialect.MySQL

hibernate.connection.url的连接属性。

我认为你没有配置hibernate.cfg.xml。

 Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); SessionFactory factory = configuration.buildSessionFactory(); 

我认为你的datasource-config.xml不在classpath中

而不是在WEB-INF/datasource-config.xml中拥有此文件

将其复制到WEB-INF/classes/datasource-config.xml

您没有发布会话工厂初始化代码。 但我想这与这个问题是一样的

从Hibernate 4.3.2.Final,引入了StandardServiceRegistryBuilder。 请按照此顺序进行初始化,例如:

 Configuration configuration = new Configuration(); configuration.configure("com/jeecourse/config/hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

请仔细检查您的代码。

我想你不会像下面那样创建配置。

 Configuration conf = new Configuration().configure(); 

最好

我认为在创建配置对象后卸载hibernate.cfg.xml文件会导致错误:

 Configuration config = new Configuration(); config.configure("hibernate.cfg.xml"); 

我有同样的问题,问题是,我的计算机没有输入postgres数据库的pg_hba.conf,允许允许哪些客户端(IP-Adresses)与数据库对话。

顺便说一句:其他用户告诉他们,如果他们忘记在本地启动数据库服务,那么错误也会引发

单独创建ServiceRegistry实例并将其传递给buildSessionFactory可能会导致此错误。 尝试创建SessionFactory实例,如下所示:

 private static SessionFactory factory; factory = cfg.buildSessionFactory( new ServiceRegistryBuilder().applySettings(cfg.getProperties()) .buildServiceRegistry());