title: Error creating bean with Controller and entityManagerFactory
body: <p>I create a project with Spring and when I compile I got this errors with Controller and entityManagerFactory. This is my first project with Spring and I don`t know what is wrong. Could you help me?</p>
  
  <pre><code> org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeesController': Unsatisfied dependency expressed through field 'employeeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Cannot create inner bean '(inner bean)#2996c83d' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2996c83d': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/pszymanski/config/DBConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
  at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
  at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
  at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
  at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
  at javax.servlet.GenericServlet.init(GenericServlet.java:158)
  at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
  at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
  at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4921)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5231)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
  at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
  at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
  at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
  at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
  at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
  at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
  at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:506)
  at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:405)
  at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1595)
  at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:280)
  at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
  at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1184)
  at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1408)
  at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1412)
  at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1380)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>pom.xml</p>
  
  <blockquote>
    <p>
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
        4.0.0</p>
  </blockquote>
  
  <pre><code>&lt;groupId&gt;pszymanski&lt;/groupId&gt;
  &lt;artifactId&gt;employees&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;properties&gt;
      &lt;spring.version&gt;4.3.7.RELEASE&lt;/spring.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&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;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
          &lt;version&gt;1.11.8.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
          &lt;version&gt;4.2.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;4.2.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-taglibs&lt;/artifactId&gt;
          &lt;version&gt;4.2.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!---Hibernate --&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.12.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;5.2.12.Final&lt;/version&gt;
      &lt;/dependency&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.2.Final&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.5&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;jstl&lt;/groupId&gt;
          &lt;artifactId&gt;jstl&lt;/artifactId&gt;
          &lt;version&gt;1.2&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.7.25&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p></p>
  
  <p>Database Configuration:</p>
  
  <pre><code>@Configuration
  @PropertySource(value = {"classpath:db.properties"})
  @EnableJpaRepositories(basePackages = {"com.pszymanski.repository"})
  public class DBConfig {
      @Autowired
      private Environment environment;
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driver.class.name")); //hibernate configuration for database
          dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
          dataSource.setUsername(environment.getRequiredProperty("jdbc.user.name"));
          dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
  
          return dataSource;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() {
          EntityManagerFactory factory = entityManagerFactory();
          return new JpaTransactionManager(factory);
      }
  
      @Bean
      public EntityManagerFactory entityManagerFactory() {
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setGenerateDdl(Boolean.TRUE);
          vendorAdapter.setShowSql(Boolean.TRUE);
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setPackagesToScan("com.pszymanski.model");
          factory.setDataSource(dataSource());
          factory.afterPropertiesSet();
          factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
          return factory.getObject();
      }
      @Bean
      public HibernateExceptionTranslator hibernateExceptionTranslator() {
          return new HibernateExceptionTranslator();
      }
  }
  </code></pre>
  
  <p>EmployeeController:</p>
  
  <pre><code> package com.pszymanski.controller;
  import com.pszymanski.model.Employee;
  import com.pszymanski.repository.EmployeeRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import javax.validation.Valid;
  import java.util.List;
  
  @Controller
  public class EmployeesController {
  
      @Autowired
      private EmployeeRepository employeeRepository;
  
      @RequestMapping(value = "/employees", method = RequestMethod.GET)
      public String getEmployeesPage(Model model) {
  
          List&lt;Employee&gt; employeeList = employeeRepository.findAll();
          model.addAttribute("employeesList", employeeList);
  
          return  "employees";
      }
  
  
      @RequestMapping(value = "/employees-add", method = RequestMethod.GET)
      public String addEmployee(Model model) {
  
          model.addAttribute("employees", new Employee());
  
          return"employees-add";
  
      }
  
      @RequestMapping(value = "/employees-add/save", method = RequestMethod.POST)
      public String saveEmployee( @ModelAttribute @Valid Employee employee, BindingResult result) {
  
          if(result.hasErrors()) {
              return "employees-add";
          }
  
          employeeRepository.save(employee);
  
          return"redirect:/employees";
      }
  
      @RequestMapping(value = "/employees-add/cancel", method = RequestMethod.GET)
      public String cancel() {
  
          return "redirect:/employees";
      }
  
      @RequestMapping(value = "/employees/edit/{id}", method = RequestMethod.GET)
      public String editEmployee(@PathVariable Long id, Model model){
  
          Employee employee = employeeRepository.findOne(id);
          model.addAttribute("employee", employee);
          return "employee-add";
      }
  
      @RequestMapping(value ="/employees/delete/{id}", method = RequestMethod.POST)
      public String deleteEmployee(@PathVariable Long id) {
  
          employeeRepository.delete(id);
          return "redirect:/employees";
      }
  }
  </code></pre>
  
  <p>EmployeeRepository:</p>
  
  <pre><code> package com.pszymanski.repository;
  
  import com.pszymanski.model.Employee;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface EmployeeRepository extends JpaRepository&lt;Employee,Long&gt; {
  }
  </code></pre>