我正在尝试将 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 时,我都会在
sessionFactory.getCurrentSession().save(emp1);

EmployeeDAO 类。

现在我的表是空的,我正在尝试在其中添加一条记录。我不知道我所做的确切错误是什么。我相信应该有一个愚蠢的。有人可以对此有所了解。谢谢您的帮助!

最佳答案

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

关于spring - getcurrentsession 中的空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14583961/

10-16 21:53