title: Spring OpenSessionInViewFilter and sessionFactory bean not found
body: <p>I'm trying to add a OpenSessionInViewFilter in my Spring MVC application because accessing a collections in my Thymeleaf templates resulted in a LazyInitializationException. This is what I have:</p>
  
  <pre><code>public class ApplicationInitializer implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(ApplicationContext.class);
          rootContext.setDisplayName("Test");
  
          servletContext.addListener(new ContextLoaderListener(rootContext));
  
          ServletRegistration.Dynamic dispatcher =
                  servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
  
          FilterRegistration.Dynamic filter = servletContext.addFilter("openSessionInViewFilter", OpenSessionInViewFilter.class);
          filter.setInitParameter("singleSession", "true");
          filter.addMappingForServletNames(null, true, "dispatcher");
  
      }
  
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan
  @Import({ SecurityConfig.class })
  @PropertySource("classpath:application.properties")
  @EnableJpaRepositories("test.model")
  public class ApplicationContext extends WebMvcConfigurerAdapter {
  
      @Autowired
      public StringToCompany stringToCompany;
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/**");
          registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/**");
          registry.addResourceHandler("/images/**").addResourceLocations("/resources/images/**");
      }
  
      @Override
      public void addFormatters(FormatterRegistry registry) {
          registry.addConverter(stringToCompany);
      }
  
      @Bean
      public SessionBean sessionBean() {
          return new SessionBean();
      }
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource datasource = new DriverManagerDataSource();
          ...
          return datasource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
          LocalContainerEntityManagerFactoryBean factoryBean
                  = new LocalContainerEntityManagerFactoryBean();
          factoryBean.setDataSource( dataSource() );
          factoryBean.setPackagesToScan( new String[ ] { "test.model" } );
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          factoryBean.setJpaVendorAdapter( vendorAdapter );
          factoryBean.setJpaProperties( this.additionalProperties() );
          return factoryBean;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(
                  this.entityManagerFactory().getObject() );
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      Properties additionalProperties() {
          Properties properties = new Properties();
          ...
          return properties;
      }
  
  }
  </code></pre>
  
  <p>This results in</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
  </code></pre>
  
  <p>I was hoping it would be as sample as adding this:</p>
  
  <pre><code>@Bean
  public AnnotationSessionFactoryBean sessionFactory() {
      return new AnnotationSessionFactoryBean();
  }
  </code></pre>
  
  <p>but that causes a while bunch of</p>
  
  <pre><code>java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
  </code></pre>
  
  <p>I finally tried changing AnnotationSessionFactoryBean to LocalSessionFactoryBean (Hibernate 4) but that gave me</p>
  
  <pre><code>Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  </code></pre>
  
  <p>Can anyone help me integrate OpenSessionInViewFilter into my current setup? I'm trying not to use xml.</p>