0

I am new to hibernate and I am trying to integrate spring servlets with hibernate. I've been trying to solve this NullPointerException the whole day.

This is my HelpDesk-servlet

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd 
   http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"

        default-autowire="byName">

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.pack.model" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>jdbc.properties</value>
        </property>
    </bean>

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

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.pack.model" /> 
        <property name="configLocation">
            <value>resources/hibernate.cfg.xml</value>
        </property>

        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
            </props>
        </property>
    </bean>

    <bean id="check" class="com.pack.model.check">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

And I have this hibernate.cfg.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.pack.model.LoginModel" />
        <mapping class="com.pack.model.TicketModel"/>
        <mapping class="com.pack.model.check"/>
    </session-factory>
</hibernate-configuration>

And this is the check.java class where I am trying to autowire the sessionFactory bean. I get The nullPointerException at the .getCurrentSession() line

@Repository
public class check {

    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getFactory() {
        return sessionFactory;
    }

    public void setFactory(SessionFactory factory) {
        this.sessionFactory = factory;
    }

    public void write(){
        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();
        TicketModel model = new TicketModel();
        model.setAdminComment("check");
        model.setDate("The date");
        model.setDescription("The description");
        model.setPriority("priority one");
        model.setStatus("Open");
        model.setSubject("SubjectLine");

        LoginModel m = new LoginModel();
        ArrayList<TicketModel> ticks = new ArrayList<TicketModel>();
        m.setTickets(ticks);
        m.setFullnames("John Smith");
        m.setPassword("passw");
        m.setRole("Test");
        m.setUsername(1245);

        session.save(m);

        session.getTransaction().commit();
        sessionFactory.close();
        session.close();
    }
}

And this is the class I am trying to run my project

public class RunTest {

    public static void main(String[] args){
        check n = new check();
        n.write();
    }
}

I am stuck please someone help!!!

0 Answers0