我正在使用hibernate集成学习spring。我得到了这个异常

Exception in thread "main" java.lang.NullPointerException
Sep 04, 2014 7:06:50 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4577357d: startup date [Thu Sep 04 19:06:50 IST 2014]; root of context hierarchy
Sep 04, 2014 7:06:50 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [app-context.xml]
Sep 04, 2014 7:06:51 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@30583b71: defining beans [dataSource,mysessionFactory,hibernateTemplate,employeeDAO]; root of factory hierarchy
.19:06:52.016 [main] INFO  org.hibernate.cfg.Environment - Hibernate 3.3.0.SP1
.19:06:52.023 [main] INFO  org.hibernate.cfg.Environment - hibernate.properties not found
.19:06:52.027 [main] INFO  org.hibernate.cfg.Environment - Bytecode provider name : javassist
.19:06:52.032 [main] INFO  org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
.19:06:52.246 [main] INFO  org.hibernate.cfg.HbmBinder - Mapping class: com.bean.Employee -> EMP1
Sep 04, 2014 7:06:52 PM org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory
INFO: Building new Hibernate SessionFactory
.19:06:52.320 [main] INFO  o.h.c.ConnectionProviderFactory - Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
.19:06:54.660 [main] INFO  org.hibernate.cfg.SettingsFactory - RDBMS: Oracle, version: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
.19:06:54.660 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC driver: Oracle JDBC driver, version: 10.2.0.1.0XE
.19:06:54.681 [main] INFO  org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.Oracle9Dialect
.19:06:54.683 [main] WARN  org.hibernate.dialect.Oracle9Dialect - The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
.19:06:54.686 [main] INFO  o.h.t.TransactionFactoryFactory - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
.19:06:54.688 [main] INFO  o.h.t.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
.19:06:54.688 [main] INFO  org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
.19:06:54.688 [main] INFO  org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
.19:06:54.688 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
.19:06:54.688 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
.19:06:54.689 [main] INFO  org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
.19:06:54.689 [main] INFO  org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
.19:06:54.689 [main] INFO  org.hibernate.cfg.SettingsFactory - Connection release mode: auto
.19:06:54.690 [main] INFO  org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
.19:06:54.690 [main] INFO  org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
.19:06:54.690 [main] INFO  org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
.19:06:54.690 [main] INFO  org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
.19:06:54.690 [main] INFO  org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
.19:06:54.692 [main] INFO  o.h.h.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
.19:06:54.692 [main] INFO  org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
.19:06:54.693 [main] INFO  org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
.19:06:54.693 [main] INFO  org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
.19:06:54.693 [main] INFO  org.hibernate.cfg.SettingsFactory - Query cache: disabled
.19:06:54.693 [main] INFO  org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
.19:06:54.693 [main] INFO  org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
.19:06:54.693 [main] INFO  org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
.19:06:54.699 [main] INFO  org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
.19:06:54.700 [main] INFO  org.hibernate.cfg.SettingsFactory - Statistics: disabled
.19:06:54.700 [main] INFO  org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
.19:06:54.700 [main] INFO  org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
.19:06:54.700 [main] INFO  org.hibernate.cfg.SettingsFactory - Named query checking : enabled
.19:06:54.771 [main] INFO  o.hibernate.impl.SessionFactoryImpl - building session factory
.19:06:55.035 [main] INFO  o.h.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
1
rajeev
Exception in thread "main" java.lang.NullPointerException
    at com.bean.EmployeeDAO.saveEmployee(EmployeeDAO.java:15)
    at com.bean.Test.main(Test.java:13)

我找不到错误,有人能帮我吗?下面是我的代码
我已经创建了这样的数据库表
CREATE TABLE  EMP1
(
    ID NUMBER(10,0) NOT NULL PRIMARY KEY,
    NAME VARCHAR2(10)
);

app-context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" id="dataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
        <property name="username" value="test"></property>
        <property name="password" value="test"></property>
    </bean>

    <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
        id="mysessionFactory">

        <property name="dataSource" ref="dataSource"></property>
        <property name="mappingResources">
            <list>
                <value>Employee.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.orm.hibernate3.HibernateTemplate"
        id="hibernateTemplate">
        <property name="sessionFactory" ref="mysessionFactory"></property>
    </bean>

    <bean class="com.bean.EmployeeDAO" id="employeeDAO">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
</beans>

employeedao.java公司
package com.bean;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class EmployeeDAO {
    HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public void saveEmployee(Employee employee){
        System.out.println(employee.getId());
        System.out.println(employee.getName());
        hibernateTemplate.save(employee);
    }

    public void updateEmployee(Employee employee){
        hibernateTemplate.update(employee);
    }
}

雇员.java
package com.bean;

public class Employee {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

employee.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 3, 2014 9:01:05 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.bean.Employee" table="EMP1">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
    </class>
</hibernate-mapping>

测试.java
package com.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("app-context.xml");
        Employee employee=new Employee();
        employee.setName("rajeev");
        employee.setId(1);
        EmployeeDAO employeeDAO=new EmployeeDAO();
        employeeDAO.saveEmployee(employee);
    }
}

这是github link
[email protected]:mcarajeev/spring-with-hibernate1.git

最佳答案

如果以这种方式创建EmployeeDAO employeeDAO=new EmployeeDAO();,则hibernatemplate属性为空。
尝试改变
EmployeeDAO employeeDAO=new EmployeeDAO();
在里面
EmployeeDAO employeeDAO= (EmployeeDAO) context.getBean("employeeDAO");

关于java - 带有Hibernate的Spring-线程“main”中的异常java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25667491/

10-11 17:40