32

having more issues with seting up hibernate with spring3. this time it is saying that connection is nul as the dialect is not set which it is on my hibernate.cfg.xml file.

here is the full exception:

    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

Here is my dataSource-config.xml thats ets up the sessionfactory

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
      ">


    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password} " />
    </bean>

    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.jr.freedom"/>
        <property name="hibernateProperties" value="classpath:hibernate.cfg.xml"/>

    </bean>

    <!-- Declare a transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        p:sessionFactory-ref="mySessionFactory" />

</beans>

And below is the hibernate.cfg.xml file

 <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- JDBC connection settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/freedom</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>

        <!-- JDBC connection pool, use Hibernate internal connection pool -->
        <property name="connection.pool_size">25</property>

        <!-- Defines the SQL dialect used in Hiberante's application -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Display and format all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Mapping to hibernate mapping files -->
        <!--mapping resource="org/kodejava/example/hibernate/app/Label.hbm.xml"/-->
    </session-factory>
</hibernate-configuration>

As you can see, the dialect is being set.

edit: my database.properties file

    # 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

edit: here is a full stack trace. accessing the database could be the issue but i can succefully access it via command prompt?

   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]
Jono
  • 17,341
  • 48
  • 135
  • 217
  • i dont think u can debug an xml file. i have tried debugging the actual java code but it never reaches it as it is still trying to organise the beans and its applicaion context – Jono Jun 26 '12 at 16:17
  • Spring and Hibernate are written in "actual java code", too. The stacktrace reveals which classes and methods are involved in building the SessionFactory. Simply set a few breakpoint there to find out where things are going wrong? – meriton Jun 26 '12 at 16:20
  • Sorry but u cant debug libs unless u import the whole lib class project into your project. things like "org.springframework.beans.factory" cant be debugged through libs. – Jono Jun 27 '12 at 08:58
  • 2
    Of course you can. Yes, the source code of the libraries is useful for that. A well configured IDE / build tool like eclipse/maven will download the source code of dependencies automatically. Failing that, you can also attach the sources manually. In short, consult the documentation of your IDE before proclaiming something impossible. – meriton Jun 27 '12 at 16:33

17 Answers17

14

I had this error with JPA, then I just added this line to application.properties:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

based on this post: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Luiz Rossi
  • 772
  • 5
  • 19
11

The database connection is missing. Add to your hibernate.cfg.xml file a lines like this

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
    jdbc:mysql://localhost:3306/your_database
</property>
<property name="connection.username">your_user</property>
<property name="connection.password">your_password</property>

(replace localhost if the database is not installed on your computer, and set the values beginning with your_ in my example).

Johanna
  • 5,223
  • 1
  • 21
  • 38
  • Ok cool. i had those properties defined in a .properties file. is that incorrect? Also, the database connections are defined on one of my beans called datasource – Jono Jun 27 '12 at 08:59
  • 1
    Hibernate uses different properties than you do (Hibernate uses `connection.url`, you are using `database.url`). Is your properties file perhaps for jdbc or for spring (I'm not good with spring)?. I think mixing these two types of configuration files is the problem, because then Hibernate sees only half of them. – Johanna Jun 27 '12 at 09:07
  • its for spring. i hvae followed a guide for spring+hibernate that has the exact same thing setup and also read spring in action book that uses a dataSource bean that hooks into the sessionFactory bean. that is what i am trying to do. to seperate the datasource and hibernate config so i can create custom build targets that automaticaly build and dump the appropriate database/hbernate settings based in the enviornment i will build apon – Jono Jun 27 '12 at 09:21
2

I had the same problem. Switching from the Apache Commons DataSource:

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password} " />
</bean>

To C3P0:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://ipadress:3306/schema" />
    <property name="user" value="root" />
    <property name="password" value="abc" />
</bean>

made my problem go away. Maybe someone can explain why, I am just happy it works and wanna share what little I know :)

Jonas Eicher
  • 1,413
  • 12
  • 18
2

I think its the problem of not loading your hibernate.cfg.xml file in your application.

I too got the same problem. I have tried with the following code in my project:

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

Then it has worked fine. Hope this will help.

Nicomedes E.
  • 1,326
  • 5
  • 18
  • 27
BALA
  • 21
  • 2
2

This issue comes if your mysql-connector version and mysql-installer version is different.

Internally it will throw Caused by:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client.

So you need make your mysql-connector version and mysql-installer version same...

DaFois
  • 2,197
  • 8
  • 26
  • 43
navin shah
  • 51
  • 3
1

I think your datasource-config.xml is not in classpath

instead of having this file in WEB-INF/datasource-config.xml

copy it to WEB-INF/classes/datasource-config.xml

Nicomedes E.
  • 1,326
  • 5
  • 18
  • 27
Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • password you have specified in property file was incorrect. I think earlier problem was solved. – Rahul Agrawal Jun 28 '12 at 09:38
  • i double checked the password and its correct. obviously i did not post real password in this code snippet but the password i use to login via cmd works for root user and i set the same password in the properties and hibernate.cfg file – Jono Jun 28 '12 at 10:06
1

I you are using hibernate and InnoDB why don't you set the hibernate.dialect to org.hibernate.dialect.MySQL5 or org.hibernate.dialect.MySQL.

and the connection property to hibernate.connection.url.

ssedano
  • 8,322
  • 9
  • 60
  • 98
  • im not using InnoDB. just using standard mysql5. Also the hibernate.connection.url is already set: jdbc:mysql://localhost:3306/freedom – Jono Jun 28 '12 at 10:15
  • but the property is connection.url. – ssedano Jun 28 '12 at 10:31
  • ahh i see what u mean. i just changed it and did not fix the issue. same issue with dialect not being set – Jono Jun 28 '12 at 10:49
1

i think you are not configure hibernate.cfg.xml.

Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory factory = configuration.buildSessionFactory();
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
manoj
  • 118
  • 8
1

Added the following in persistence.xml file,

<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
Lucky
  • 16,787
  • 19
  • 117
  • 151
0

You did't post your session factory initialization code. But I guess this is the same problem with this one

From Hibernate 4.3.2.Final, StandardServiceRegistryBuilder is introduced. Please follow this order to intialize, e.g.:

Configuration configuration = new Configuration();
configuration.configure("com/jeecourse/config/hibernate.cfg.xml");

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
        configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Please double check your code.

Community
  • 1
  • 1
Jimmy
  • 1,699
  • 2
  • 16
  • 17
0

I think you don't create the Configuration like below.

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

Best

jsina
  • 4,433
  • 1
  • 30
  • 28
0

Creation of ServiceRegistry instance separately and pass it to buildSessionFactory may cause this error. Try to create SessionFactory instance as follows:

private static SessionFactory factory;
factory = cfg.buildSessionFactory(
          new ServiceRegistryBuilder().applySettings(cfg.getProperties())
         .buildServiceRegistry());
Nicomedes E.
  • 1,326
  • 5
  • 18
  • 27
Ram
  • 1
  • 3
0

I think the error cased by unloading hibernate.cfg.xml file after you have created the Configuration Object like this:

Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
RGA
  • 2,577
  • 20
  • 38
xiaofeiyu
  • 1
  • 1
0

I had the the same problem and the issue was, that my computer was not entered in the pg_hba.conf of the postgres database that allows which Clients(IP-Adresses) are allowed to speak with the database.

btw: Other users tells that the error raises too if they simply forget starting database service locally

phil
  • 1,289
  • 1
  • 15
  • 24
0

We were recently facing the same issue with one of our deployment, as we were also using hibernate and dialect is null is a vague error. I use was with connection string itself and did the changes below to make it work

connectionUrl :- jdbc:mysql://127.0.0.1:3306/{{dbName}}?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false

Steps how did we solve it 1) We ran simple DB connection with JDBC driver that will gave the exception of time zone. 2) we found the issue with time zone and had to add more properties in connection string as show above. 3) We also added 2 other properties as still it was not working.

Dialect is not set is vague to took lot of our time, I hope this helps.

0

This worked for me added the dialect property...

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <persistence-unit name="project_name" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.schema-generation.drop-source" value="script-then-metadata"/>
            <property name="dialect" value="org.hibernate.dialect.MySQLDialect"/>  
        </properties>
    </persistence-unit>
</persistence>
SatyaRajC
  • 51
  • 7
0

I face this problem when I project update , the error is javax.persistence.jdbc.url', 'hibernate.connection.url', or 'hibernate.dialect'

and I found the solution. it work fine.

here is the details solution