sessionFactory.getCurrentSession()中的nullpointerexception;

时间:2015-09-14 14:24:09

标签: spring hibernate glassfish

我在sessionFactory.getCurrentSession()中得到错误nullpointerexception;

  • Spring Framework 4.0
  • Glassfish 4.1
  • hibernate 4.3.6.Final

当我调用Session session = sessionFactory.getCurrentSession();我在glassfish服务器日志中遇到错误,但是SetSessionFactory是由Spring设置的。我的代码有什么问题。请帮忙! :)

它是Spring @Component

package service;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Repository;

import model.Payments;

@Component
@ManagedBean(name = "paymentsService")
@ApplicationScoped
@Repository
public class PaymentsService {


    private SessionFactory sessionFactory ;

    public PaymentsService()
    {

    }


    public PaymentsService(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

   // @Transactional
    public void register(){

        Session session = sessionFactory.getCurrentSession();

    }
}

它的主要成员@ManagedBean

package backbean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.bean.SessionScoped;

import model.Payments;
import service.PaymentsService;


@ManagedBean
@ApplicationScoped
public class Paymentsbeen implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 7719279452314113041L;


    public String register() {

        paymentsService.register();


    }

}

它的Spring配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- Enable Spring Annotation Configuration -->
    <context:annotation-config />
    <!-- Scan for all of Spring components such as Spring Service -->
    <context:component-scan base-package="service.PaymentsService"></context:component-scan>

    <!-- Create Data Source bean -->
    <!--<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:jdbc/__TestPool"/>
    </bean>-->

   <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/ihomefinance" />
        <property name="username" value="postgres" />
        <property name="password" value="1" />
    </bean>

    <!-- Define SessionFactory bean -->
    <bean id="hibernate3AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="DataSource" />
        <property name="annotatedClasses">
            <list>
                <value>model.Payments</value>                
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>        
    </bean>
    <!-- Transaction Manager 
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" />
    </bean>-->

    <bean id="PaymentsServiceID" class="service.PaymentsService">
        <property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" />
    </bean>

    <!-- Detect @Transactional Annotation 
    <tx:annotation-driven transaction-manager="transactionManager" />-->

</beans>

0 个答案:

没有答案