在Spring 3.2和Hibernate 4上使用sessionfactory时出现空指针错误 [英] Null pointer error while using sessionfactory on Hibernate 4 with Spring 3.2

查看:67
本文介绍了在Spring 3.2和Hibernate 4上使用sessionfactory时出现空指针错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力找出确切的问题,为什么当我尝试从spring bean xml自动连接sessionFactory时,为什么Hibernate sessionFactory会引发Null指针错误.有人可以帮我解决这个问题.我是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();
    }

}

解决方案

我已经解决了该问题,而不是使用getter方法访问该对象,但是该方法有效,不确定其背后的真正概念是什么.

>

public String getActionCode(int id) {

    //ApplicationContext context;

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

    return actionCode.getActionName();
}

I'm struggling to figure out the exact problem, why the Null Pointer Error is thrown for Hibernate sessionFactory when I try to Auto wire sessionFactory from spring bean xml. Could somebody please help me out fix this. I'm new to Spring and deploying the app on Jboss server

Here is the 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>

Here is Hiberante.cfg.xml which I placed in webcontent folder

<!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>

And the DAO class in which I'm trying to access the sessionFactory is

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

}

解决方案

I've fixed the issue, instead of accessing the object I used the getter method and it worked, not sure what is the real concept behind it.

public String getActionCode(int id) {

    //ApplicationContext context;

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

    return actionCode.getActionName();
}

这篇关于在Spring 3.2和Hibernate 4上使用sessionfactory时出现空指针错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆