1

I have created a maven web project. The stack is Spring + hibernate + jersey.

I have this hibernate.cfg.xml under src/main/resources/ path. When I try to deploy it an tomcat I get this stacktrace,

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'WorkspacesDBSessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:917)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:860)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:775)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 24 more
Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:422)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:863)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:782)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 34 more

When i checked the DialectFactory.java:57, i found the code,

public static Dialect determineDialect(String databaseName, int databaseMajorVersion) {
    if ( databaseName == null ) {
        throw new HibernateException( "Hibernate Dialect must be explicitly set" );
    }

So the actual issue was that the, hibernate.cfg.xml was not being loaded. So I removed the file and tried the deployment. I again got the same stacktrace.

My ApplicationContext.xml is,

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org.work.autogen, org.work.db.api, org.work.services, org.work.routes">
    </context:component-scan>

    <bean id="log4jInitializer"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>classpath:log4j.properties</value>
            </list>
        </property>
    </bean>

    <bean id="WorkspacesDBSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- <property name="mappingResources">
            <list>
                <value>Appraisal.hbm.xml</value>
                <value>Managers.hbm.xml</value>
                <value>EmployeeSecretUuid.hbm.xml</value>
                <value>Work.hbm.xml</value>
                <value>Company.hbm.xml</value>
                <value>Employee.hbm.xml</value>
            </list>
        </property> -->
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.default_schema">Workspacedb</prop>
            </props>
        </property>
    </bean>
</beans>

My Hibernate.cfg.xml is,

<?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><!-- name="sessionFactory"> --> 
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Workspacedb</property>
        <property name="hibernate.connection.username">workadmin</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="Appraisal.hbm.xml" />
        <mapping resource="Managers.hbm.xml" />
        <mapping resource="EmployeeSecretUuid.hbm.xml" />
        <mapping resource="Work.hbm.xml" />
        <mapping resource="Company.hbm.xml" />
        <mapping resource="Employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>

How I do make this work?

Thanks.

Aspirant9
  • 163
  • 1
  • 13

2 Answers2

3

As per the logs, it is the LocalSessionFactoryBean that is trying to configure Hibernate.

Also in your Spring configuration file you are not mentioning anywhere that you have a separate Hibernate configuration file.

So in this code:

<bean id="WorkspacesDBSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.default_schema">Workspacedb</prop>
            </props>
        </property>
</bean>

you are just setting the hibernate properties for dialect and hibernate.default_schema.

To fix the issue change the property dialect to hibernate.dialect.

Now if you still want to use the Hibernate configuration file then you need to set the configLocation property in your spring configuration file.

Here is an example on how to use the configLocation property:

<bean id="WorkspacesDBSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

You can see detailed explanation in this post: applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

Community
  • 1
  • 1
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
0

Your hibernate.cfg.xml must be in CLASSPATH to pick up automatically. Try to put /src/main/java. Because Hibernate finds firstly in CLASSPATH default(/src/main/java)

Raphael Milani
  • 151
  • 2
  • 14
  • it is a maven project as shown in tags of this question, so keeping it in `src/main/resources` is the correct way of placing any configuration files in maven. Once the project is build, then the configuration file will be placed in the root of the classpath. – Chaitanya Apr 30 '15 at 19:39
  • But how it´s maven´s configuration, when he runs maven? – Raphael Milani Apr 30 '15 at 19:42
  • I did not understand your question, can you please elaborate? – Chaitanya Apr 30 '15 at 19:44
  • My question is: when he runs maven, how the app is packing? Because putting hibernate.cfg.xml in src/main/resources is not guaranteed that hibernate will find it to load it. – Raphael Milani Apr 30 '15 at 20:04
  • Please check maven docs on how maven works. For your question, check this post --> http://stackoverflow.com/questions/15258594/hibernate-exception-hibernate-cfg-xml-not-found – Chaitanya Apr 30 '15 at 20:10
  • Ok I got it. I thought he tried to deploy the app directly by eclipse. – Raphael Milani Apr 30 '15 at 20:15