Search code examples
springhibernatenullpointerexceptionspring-annotationssessionfactory

Spring+Hibernate configuration, null SessionFactory


I've been searching extensively for hours, seeing different scenarios with different approaches for a specific/common problem, but having no luck to solve a very simple Null SessionFactory. Here are my classes, I made it as short as possible so while I'm trying to cut down the codes I'm hoping to locate the problem. Still no luck.

The Service Class

public class Service {

  @Autowired
  SessionFactory sessionFactory;

  @Transactional
  public void doSomething() {

     Student stud = new Student();

     stud.setName("Student");
     stud.setAge(23);
     stud.setSchool("School");

     System.out.println(sessionFactory);

     sessionFactory.getCurrentSession().save(stud);
   }
}

My Entity Class (POJO)

@Entity
@Table(name = "STUDENT")
public class Student extends Person {

  @Column(name = "school")
  private String school;

  public Student() {
  }

  public Student(String school) {
     this.school = school;
  }

  public String getSchool() {
    return school;
  }

  public void setSchool(String school) {
    this.school = school;
  }
}

The configuration file (applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<tx:annotation-driven/>

<context:property-placeholder
    location="classpath:properties/database.properties"
    ignore-unresolvable="false" />

<context:component-scan base-package="edu.service"/>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>edu.domain</value>
        </list>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

And the main class

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(
            "/config/applicationContext.xml");

      Service service = new Service();
      service.doSomething();

      ((ClassPathXmlApplicationContext) context).registerShutdownHook();
      ((ClassPathXmlApplicationContext) context).close();
  } 
}

I really dont get why do I have a null SessionFactory, I tried to made it as short as possible, removed some part of the applications(DAOs,Services,Annotations), still no luck. Any help please. Thank you in Advance


Solution

  • Try this:

    First annotate or configure the service in xml. Then get it from the context like below:

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "/config/applicationContext.xml");
    
        Service service = (Service) context.getBean("service");
        service.doSomething();
    
        ((ClassPathXmlApplicationContext) context).registerShutdownHook();
        ((ClassPathXmlApplicationContext) context).close();
    }
    

    Let me know if this helps.