title: Hibernate SessionFactory annotation based configuration
body: <p>My recent goal is to build a spring boot application, but without any XML config files (or as less as possible) so I would like to avoid using some XML files (i.e. web.xml) especially for some bean definition parts.</p>
  
  <p>And here comes tougher part.</p>
  
  <p>I want to inject using @Autowired annotation a SessionFactory bean into classes, but everytime I try to start application I get:</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'temperatureController': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: EntityManagerFactory must not be null</p>
  </blockquote>
  
  <p>Ok, I understand that Spring has no SessionFactory bean because it has no EntityManagerFactory.</p>
  
  <p>So I would appreciate any suggestions how to solve this, but only with configuration by annotations.</p>
  
  <p>So far I read similar post to mine about specifying in @Configuration class a bean this way:</p>
  
  <pre><code>@Bean
  public HibernateJpaSessionFactoryBean sessionFactory() {
      return new HibernateJpaSessionFactoryBean();
  }
  </code></pre>
  
  <p>And then adding this line into properties file:</p>
  
  <blockquote>
    <p>spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext</p>
  </blockquote>
  
  <p>And finally @Autowired with SessionFactory should work good.
  But, of course for me it's not working.</p>
  
  <p>Any ideas what should I do different/better?</p>
  
  <p>My properties file is very basic:</p>
  
  <blockquote>
    <p>spring.jpa.show-sql = true
    spring.datasource.password=mysql
    spring.datasource.username=mysql
    spring.datasource.testWhileIdle = true
    spring.jpa.hibernate.ddl-auto = update
    spring.datasource.validationQuery = SELECT 1
    spring.datasource.url= jdbc:mysql://localhost:3306/sys
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext</p>
  </blockquote>