title: Hibernate+spring java configuration
body: <p>I am new to spring and hibernate, i want to configure hibernate session factory using java config. now i have an xml config but not working with java configuration
  My xml config is:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.java.*" /&gt;
  
      &lt;bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
          &lt;!-- Hibernate 4 SessionFactory Bean definition --&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.java.web.model.Employee&lt;/value&gt;
                  &lt;value&gt;com.java.web.model.Answer&lt;/value&gt;
                  &lt;value&gt;com.java.web.model.Question&lt;/value&gt;
                  &lt;value&gt;com.java.web.model.Survey&lt;/value&gt;            
              &lt;/list&gt;
  
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;                
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
   &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My java config file is:</p>
  
  <pre><code>package com.java.config;
  
  import java.io.IOException;
  import java.util.Properties;
  
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
  import org.springframework.orm.hibernate4.HibernateTransactionManager;
  import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  import org.springframework.web.servlet.view.JstlView;
  
  @EnableWebMvc
  @Configuration
  @ComponentScan({ "com.java.web.*" })
  @Import({ SecurityConfig.class })
  //@PropertySource(value = { "classpath:application.properties" })
  public class AppConfig {
  
      @Autowired
      private Environment environment;    
  
  
      @Bean
      public InternalResourceViewResolver viewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setViewClass(JstlView.class);
          viewResolver.setPrefix("/WEB-INF/pages/");
          viewResolver.setSuffix(".jsp");
          return viewResolver;
      }    
  
      @Bean(name = "dataSource")
      public DriverManagerDataSource dataSource() {
          DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
          driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
          driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
          driverManagerDataSource.setUsername("root");
          driverManagerDataSource.setPassword("12345");
          return driverManagerDataSource;
      }
  
  
     @Bean
      public LocalSessionFactoryBean sessionFactory() {
         LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
         sessionFactory.setDataSource(dataSource());
         sessionFactory.setPackagesToScan(new String[] { "com.java.model" });
         sessionFactory.setHibernateProperties(hibernateProperties());
  
         return sessionFactory;
      }
  
      private Properties hibernateProperties() {
          Properties properties = new Properties();
          properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
          properties.put("hibernate.show_sql", "true");
          properties.put("hibernate.format_sql", "true");
          return properties;        
      }
  
      @Bean
      @Autowired
      public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)
      {
          HibernateTransactionManager htm = new HibernateTransactionManager();
          htm.setSessionFactory(sessionFactory);
          return htm;
      }
  
  }
  </code></pre>
  
  <p>But it shows resource not available. Please help me......</p>
  
  <p><strong>Edit</strong></p>
  
  <pre><code>package com.java.config;
  
  import java.io.IOException;
  import java.util.Properties;
  
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
  import org.springframework.orm.hibernate4.HibernateTransactionManager;
  import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  import org.springframework.web.servlet.view.JstlView;
  
  @EnableWebMvc
  @Configuration
  @ComponentScan({ "com.java.web.*" })
  @Import({ SecurityConfig.class })
  //@PropertySource(value = { "classpath:application.properties" })
  public class AppConfig {
  
      @Autowired
      private Environment environment;    
  
  
      @Bean
      public InternalResourceViewResolver viewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setViewClass(JstlView.class);
          viewResolver.setPrefix("/WEB-INF/pages/");
          viewResolver.setSuffix(".jsp");
          return viewResolver;
      }    
  
      @Bean(name = "dataSource")
      public DriverManagerDataSource dataSource() {
          DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
          driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
          driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
          driverManagerDataSource.setUsername("root");
          driverManagerDataSource.setPassword("12345");
          return driverManagerDataSource;
      }
  
  
  /*    @Bean
      public LocalSessionFactoryBean sessionFactory() {
         LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
         sessionFactory.setDataSource(dataSource());
         sessionFactory.setPackagesToScan(new String[] { "com.java.model" });
         sessionFactory.setHibernateProperties(hibernateProperties());
  
         return sessionFactory;
      }*/
  
      private Properties hibernateProperties() {
          Properties properties = new Properties();
          properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
          properties.put("hibernate.show_sql", "true");
          properties.put("hibernate.format_sql", "true");
          return properties;        
      }
  
  /*    @Bean
      @Autowired
      public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)
      {
          HibernateTransactionManager htm = new HibernateTransactionManager();
          htm.setSessionFactory(sessionFactory);
          return htm;
      }*/
  
  }
  </code></pre>
  
  <p>When i comment two methods as shown above. Its working. But i need hibernate configuration.</p>