title: Error while integrating spring with hibernate
body: <p>I'm trying to integrate spring with hibernate but i get this error:</p>
  
  <blockquote>
    <p>[2017-10-06 05:26:14,475] Artifact spring-mvc-hibernate-example:war: <code>java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.boraji.tutorial.spring.config.AppConfig: Invocation of init method failed; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution. Please see server.log for more details.</code></p>
  </blockquote>
  
  <p>Here are some files:</p>
  
  <p><strong>pom.xml</strong>:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.boraji.tutorial.spring&lt;/groupId&gt;
    &lt;artifactId&gt;spring-mvc-hibernate-example&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;properties&gt;
      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;
      &lt;!-- Spring MVC Dependency --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
        &lt;version&gt;4.3.7.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Spring ORM --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
        &lt;version&gt;4.3.7.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Mysql Connector --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;mysql&lt;/groupId&gt;
        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
        &lt;version&gt;6.0.6&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Hibernate ORM --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
        &lt;version&gt;5.2.10.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Hibernate-C3P0 Integration --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
        &lt;version&gt;5.2.10.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- c3p0 --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;com.mchange&lt;/groupId&gt;
        &lt;artifactId&gt;c3p0&lt;/artifactId&gt;
        &lt;version&gt;0.9.5.2&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Hibernate Validator --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
        &lt;version&gt;5.4.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- JSTL Dependency --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;javax.servlet.jsp.jstl&lt;/groupId&gt;
        &lt;artifactId&gt;javax.servlet.jsp.jstl-api&lt;/artifactId&gt;
        &lt;version&gt;1.2.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;taglibs&lt;/groupId&gt;
        &lt;artifactId&gt;standard&lt;/artifactId&gt;
        &lt;version&gt;1.1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Servlet Dependency --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
        &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
        &lt;version&gt;3.1.0&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- JSP Dependency --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
        &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
        &lt;version&gt;2.3.1&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate&lt;/artifactId&gt;
        &lt;version&gt;3.5.4-Final&lt;/version&gt;
      &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
            &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
            &lt;version&gt;1.8.0-alpha2&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Initializer:</p>
  
  <pre><code>public class MyWebAppInitializer
        extends AbstractAnnotationConfigDispatcherServletInitializer {
  
     @Override
     protected Class&lt;?&gt;[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
     }
  
     @Override
     protected Class&lt;?&gt;[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
     }
  
     @Override
     protected String[] getServletMappings() {
        return new String[] { "/" };
     }
  }
  </code></pre>
  
  <p><strong>application config</strong>:</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:db.properties")
  @EnableTransactionManagement
  @ComponentScans(value = { @ComponentScan("com.boraji.tutorial.spring.dao"),
        @ComponentScan("com.boraji.tutorial.spring.service") })
  public class AppConfig {
  
     @Autowired
     private Environment env;
  
     @Bean
     public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
  
        Properties props = new Properties();
        // Setting JDBC properties
        props.put(DRIVER, env.getProperty("mysql.driver"));
        props.put(URL, env.getProperty("mysql.url"));
        props.put(USER, env.getProperty("mysql.user"));
        props.put(PASS, env.getProperty("mysql.password"));
  
        // Setting Hibernate properties
        props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
        props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
        props.put(DIALECT, env.getProperty("hibernate.dialect"));
  
        // Setting C3P0 properties
        props.put(C3P0_MIN_SIZE, 
              env.getProperty("hibernate.c3p0.min_size"));
        props.put(C3P0_MAX_SIZE, 
              env.getProperty("hibernate.c3p0.max_size"));
        props.put(C3P0_ACQUIRE_INCREMENT,
              env.getProperty("hibernate.c3p0.acquire_increment"));
        props.put(C3P0_TIMEOUT, 
              env.getProperty("hibernate.c3p0.timeout"));
        props.put(C3P0_MAX_STATEMENTS, 
              env.getProperty("hibernate.c3p0.max_statements"));
  
        factoryBean.setHibernateProperties(props);
        factoryBean.setAnnotatedClasses(User.class);
        return factoryBean;
     }
  
     @Bean
     public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
     }
  }
  </code></pre>
  
  <p>db.properties:</p>
  
  <pre><code># MySQL properties
  mysql.driver=com.mysql.jdbc.Driver
  mysql.url=jdbc:mysql://localhost/test
  mysql.user=root
  mysql.password=root
  
  # Hibernate propert[2017-10-06 05:26:14,475] Artifact spring-mvc-hibernate-example:war: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.boraji.tutorial.spring.config.AppConfig: Invocation of init method failed; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution. Please see server.log for more details.ies
  hibernate.show_sql=true
  hibernate.hbm2ddl.auto=update
  hibernate.dialect=org.hibernate.dialect.MySQLDialect
  
  #C3P0 properties
  hibernate.c3p0.min_size=5
  hibernate.c3p0.max_size=20
  hibernate.c3p0.acquire_increment=1
  hibernate.c3p0.timeout=1800
  hibernate.c3p0.max_statements=150
  </code></pre>
  
  <p>UserDao:</p>
  
  <pre><code>@Repository
  public class UserDaoImp implements UserDao {
  
     @Autowired
     private SessionFactory sessionFactory;
  
  
     public void save(User user) {
        sessionFactory.getCurrentSession().save(user);
     }
  
  
     public List&lt;User&gt; list() {
        @SuppressWarnings("unchecked")
        TypedQuery&lt;User&gt; query = sessionFactory.getCurrentSession().createQuery("from User");
        return query.getResultList();
     }
  
  }
  </code></pre>
  
  <p>I saw lots of similar problems here but it didn't help. What could go wrong in my application?</p>