Nullpointer exception in getcurrentsession

I am trying to integrate Hibernate 4.1 with Spring 3.1.3.

//configuration file (beans.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <bean id="employeeDAObean" class="com.Hib_Spring.EmployeeDAO"> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSourceBean"></property> <property name="annotatedClasses" value="com.Hib_Spring.Employee"> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="url" value="jdbc:postgresql://localhost:5432/newdatabase"/> <property name="username" value="postgres"/> <property name="password" value=" P@ssword "/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> </beans> 

This is my model class.

 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="employee_new") public class Employee { @Id @Column(name="ID") private int id; @Column (name="firstname") private String first_name; @Column(name="lastname") private String last_name; public Employee() {} public Employee(int id,String fname, String lname) { this.first_name = fname; this.last_name = lname; } public int getId() { return id; } public void setId( int id ) { this.id = id; } public String getFirstName() { return first_name; } public void setFirstName( String first_name ) { this.first_name = first_name; } public String getLastName() { return last_name; } public void setLastName( String last_name ) { this.last_name = last_name; } } 

Below is my DAO class

  import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class EmployeeDAO { @Autowired SessionFactory sessionFactory; public void createEmployee(int id, String fname, String lname) { Employee emp1 = new Employee(id,fname,lname); sessionFactory.getCurrentSession().save(emp1); } public List getAllEmployees(){ return sessionFactory.getCurrentSession().createQuery("from employee_new").list(); } } 

My table has three column identifiers, first name, last name

This is the main class (Client.java)

 import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String arg[]){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); EmployeeDAO employeedao=context.getBean("employeeDAObean",EmployeeDAO.class); System.out.println("Entering data in the database"); employeedao.createEmployee(101, "aaa", "bbb"); employeedao.createEmployee(102, "ccc", "ddd"); System.out.println("Reading the data"); List employeelist= employeedao.getAllEmployees(); for(int i=0;i<employeelist.size();i++){ Employee e = (Employee) employeelist.get(i); System.out.println("Emp No "+e.getId()); System.out.println("FirstName "+e.getFirstName()); System.out.println("LastName "+e.getLastName()); } } } 

Now my problem is that whenever I try to execute client.java I get a nullpointer exception in

 sessionFactory.getCurrentSession().save(emp1); 

class EmployeeDAO.

Now my table is empty and I'm trying to add a record to it. I am not making the exact mistake I made. I'm sure it should be stupid. Can someone shed light on him. Thanks for the help!

+4
source share
1 answer

You have tx: annotation, but I don't see any transaction annotations. Try putting the annotation in the createEmployee dao method and this will help.

+1
source

All Articles