0

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

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Jeff
  • 519
  • 1
  • 10
  • 18

2 Answers2

0

The problem lies in new Service() so the object you've created is not managed by Spring. You never called setSessionFactory() and so SessionFactory is of course null, hence an NPE when you try to reference it.

You probably want to be getting an Service managed by the Spring container which means you need to inject it somehow. So try injecting the bean Service and then call service.doSomething();

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • thank you. I was not paying too much attention on the simple details, I was focused on designing patterns. >.< :) – Jeff Aug 21 '14 at 03:35
  • I wish I can accept all answers, but I can only click one >.<, I'm just new to S.O. – Jeff Aug 21 '14 at 04:04
0

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.

Atul
  • 2,673
  • 2
  • 28
  • 34
  • 1
    That's the first step, but it seems there's another problem : for the `@Transactional` annotation to work, the service must implement an interface and be used through this interface. – Serge Ballesta Aug 20 '14 at 17:55
  • what silly mistake on my part, I was really focused on studying the DAO/Service design pattern, I lost my attention to these kind of details >.< , It worked, thank you so much.. !! – Jeff Aug 21 '14 at 02:25
  • @Jeff Happy to help. It is so trivial that it doesn't deserve a quote per say. But still here it is: "The greatest difficulties lie where we are not looking for them." - Johann Wolfgang von Goethe – Atul Aug 21 '14 at 03:18
  • @SergeBallesta actually there is(are), Im working on a DAO/Service pattern(this code is part of it) but I did not included it in here as it might cause irrelevance, so I tried so make my code SSCE(just following some general programming forum rules), I might just create another topic for it. – Jeff Aug 21 '14 at 03:31
  • @Atul Thank you for the quote!, I'll take note of it :) – Jeff Aug 21 '14 at 03:32