getcurrentsession中的nullpointer异常

时间:2013-01-29 13:23:09

标签: spring hibernate hibernate-session

我正在尝试将Hibernate 4.1与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>

这是我的模特课

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;
   }

}

以下是我的DAO课程

 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();

}
}

我的表有三列ID,名字,姓氏

这是主类(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());

    }


}
}

现在我的问题是,每当我尝试执行client.java时,我都会在

中获得nullpointer异常
sessionFactory.getCurrentSession().save(emp1);
EmployeeDAO类的

现在我的桌子是空的,我正在尝试添加一条记录。我不知道我做错了什么。我相信应该有一个愚蠢的。有人会对它有所启发吗?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

你有tx:annotation-driven但我没有看到任何交易注释。尝试将注释放在dao的createEmployee方法上,这样就可以了。