java - 在带有 Spring 3.2 的 Hibernate 4 上使用 sessionfactory 时出现空指针错误

标签 java spring hibernate java-ee-6 jboss7.x

我正在努力找出确切的问题,为什么当我尝试从 spring bean xml 自动连接 sessionFactory 时,Hibernate sessionFactory 会抛出空指针错误。有人可以帮我解决这个问题吗?我是 Spring 新手,正在 Jboss 服务器上部署应用程序

这是 Spring-Config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    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.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org.kp.db.dao.impl" />


    <bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ebm" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="configLocation" value="classpath:/hibernate.cfg.xml" />
    </bean>

    <tx:annotation-driven  transaction-manager="transactionManager" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

这是我放置在 webcontent 文件夹中的 Hiberante.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ebm</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="org/kp/db/model/Actioncode.hbm.xml"/>
    <mapping class="org.kp.db.model.TblEBMFieldDetails"/>
    <mapping class="org.kp.db.model.TbleOLI"/>
  </session-factory>
</hibernate-configuration>

我尝试访问 sessionFactory 的 DAO 类是

package org.kp.db.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.kp.db.dao.ActionCodeDao;
import org.kp.db.model.Actioncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Administrator
 */
@Repository
public class ActionCodeDaoImpl implements ActionCodeDao{

    @Override
    public String[] getActionCodes() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Autowired(required=true)
    SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


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

    @Override
    @Transactional(readOnly=true,propagation=Propagation.REQUIRED)
    public String getActionCode(int id) {

        //ApplicationContext context;

        Session  session = sessionFactory.getCurrentSession();
        Actioncode actionCode=null;
        actionCode = (Actioncode)session.get(Actioncode.class, 1);

        return actionCode.getActionName();
    }

}

最佳答案

@Named ("kpAction") 
public class Actions { 
    public String getActionCode() { 
        ActionCodeDao action =new ActionCodeDaoImpl(); 
        String str = action.getActionCode(1); System.out.println(str); return str; 
    } 
} 

当您运行这行代码时: ActionCodeDao action =new ActionCodeDaoImpl(); 您将获得一个不受 Spring 管理的对象,因此不会包含 Autowiring 的依赖项。

我想说这就是你看到空指针的原因。

但是你说你现在已经修复了它,不确定你是否也无意中更改了它,如果不是,我不确定你是如何修复它的:)

关于java - 在带有 Spring 3.2 的 Hibernate 4 上使用 sessionfactory 时出现空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14953410/

相关文章:

java - 如何将 Web 生成的内容导入到 Java 应用程序中?

java - 保存多个多对一关系时的 Hibernate 优化

java - 从后台 worker 更新 UI

java - 静态类实例变量的释放

java - Spring Bean 对象编码(marshal)取决于 XML 中给出的顺序?

java - Spring 安全: Authentication results in HTTP 405 "Method not allowed"

java - 在 Java 8 中避免多个并行 if else 循环的最佳方法

java - Apache 光束 : No Runner was specified and the DirectRunner was not found on the classpath

java - 使用 Spring Security 在运行时切换身份验证方法?

java - Hibernate 和 NonUniqueObjectException