title: How to create SessionFactory using Hibernate4 and Spring?
body: <p>I am having issues trying to manage hibernate to work with spring. I don't know how to initiate a Session class on my code. NetBeans keep me saying that the variable sessionFactory has not been initialized.<br/>
  The problem I am having is on HibernateTest.java when I try sessionFactory.openSession();</p>
  
  <p>Here is the structure of the project and the code, I hope you can help me, thank you.
  PS. I am using NetBeans 8.0.1</p>
  
  <p>Structure (Dont have rep to upload an image so ill copy as it follows):<br/>
  -Hibernate Course<br/>
    &nbsp;&nbsp;+Web Pages
    &nbsp;&nbsp;-Source Packages<br/>
      &nbsp;&nbsp;&nbsp;&nbsp;-com.course.entity<br/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gender.java<br/>
      &nbsp;&nbsp;-com.source.test<br/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HibernateTest.java<br/>
  &nbsp;&nbsp;  -Other Sources<br/>
  &nbsp;&nbsp;&nbsp;&nbsp;    -src/main/resources<br/>
      &nbsp;&nbsp;  -&lt;>default package&lt;><br/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  hibernate.cfg.xml<br/>
  &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       spring.xml<br/>
  &nbsp;&nbsp;  +Dependencies<br/>
  &nbsp;&nbsp;  +Java Dependencies<br/>
  &nbsp;&nbsp;  +Project files</p>
  
  <p>spring.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.3.xsd"&gt;
  
      &lt;bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven /&gt;
      &lt;bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;bean id="userDao" class="com.course.entity.Gender"&gt;
          &lt;constructor-arg&gt;
              &lt;ref bean="sessionFactory" /&gt;
          &lt;/constructor-arg&gt;
      &lt;/bean&gt;    
  &lt;/beans&gt;
  </code></pre>
  
  <p>Hibernate.cfg.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQL9Dialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:postgresql://localhost:5432/Hibernate&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;postgres&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;admin&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
      &lt;mapping class="com.course.entity.Gender"/&gt;
  
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Gender.java</p>
  
  <pre><code>package com.course.entity;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.SequenceGenerator;
  
  @Entity
  public class Gender {
  
      @Id
      @SequenceGenerator(name = "ggender_cod_seq", initialValue = 0)
      @GeneratedValue(generator = "gender_cod_seq", strategy = GenerationType.AUTO)
      private int codeGender;
  
      private String gender;
  
      public int getCodeGender() {
          return codeGender;
      }
  
      public void setCodeGender(int codeGender) {
          this.codeGender = codeGender;
      }
  
      public String getGender() {
          return gender;
      }
  
      public void setGender(String gender) {
          this.gender = gender;
      } 
  }
  </code></pre>
  
  <p>HibernateTest.java</p>
  
  <pre><code>package com.source.test;
  
  import com.course.entity.Gender;
  import java.text.ParseException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  
  public class HibernateTest {
  
      public static void main(String[] args) throws ParseException {
  
          Gender gender = new Gender();
  
          gender.setCodeGender(3);
  
          gender.setGender("Test");
  
          SessionFactory sessionFactory;
  
          Session session = sessionFactory.openSession();
  
          session.beginTransaction();
  
          session.save(gender);
  
          session.getTransaction().commit();
  
          session.close();
      }
  }
  </code></pre>