Spring AnnotationSessionFactoryBean NullPointerException

时间:2013-08-14 11:25:39

标签: java spring hibernate

我是Spring的新手并尝试集成Spring和Hibernate。但我在SessionFactory中面临nullpointer。

错误说明:

java.lang.NullPointerException
com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:29)
com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:34)
com.ume.LoginController.getUserCredentials(LoginController.java:39)

另一个错误说明

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
     org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:31)
com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:37)
com.ume.service.UserServiceImpl.getUser(UserServiceImpl.java:23)

我的dispatchar-servlet.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:p="http://www.springframework.org/schema/p"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.nptest" />
<mvc:annotation-driven />


  <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"    value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean class="org.springframework.context.support.ResourceBundleMessageSource"  id="messageSource">
    <property name="basename" value="messages" />
</bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.nptest.pojo.UserPojo</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>

    <bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>     



</beans>

我的DAOImplementation类

    @Repository
public class UserDaoImpl implements UserDao{

@Autowired
    private SessionFactory sessionFactory;

 private Session openSession()
 {

     return sessionFactory.getCurrentSession();
    }
     public UserPojo getUser(String username, String password) 
    {
        List<UserPojo> userlist=new ArrayList<UserPojo>();
        Query query=openSession().createQuery("from UserPojo u WHERE u.login=:username AND u.userpwd=:password");
        query.setParameter("login",username);
     query.setParameter("password",password);
        System.out.println("MADAN Query "+ query);
        userlist=query.list();
        if(userlist.size()>0)
            return userlist.get(0);
     else return null;

  }

}

我的服务界面

public interface UserService {
public UserPojo getUser(String username,String password);

 }

我的服务实现类

    @Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public UserPojo getUser(String username, String password) {
        return userDao.getUser(username, password);
 }

我从这里调用的控制器类,我已经通过这个

实例化了Service类
@Autowired
private UserService userservice;

在我打电话的方法中

 UserPojo user=userservice.getUser(username, password);

1 个答案:

答案 0 :(得分:1)

看起来你已经手动实例化了UserDaoImpl,sessionFactory没有连线,请发布代码如何使用UserDaoImpl

将注释添加到 UserDaoImpl 并将其放在包 com.nptest

package com.nptest.dao;

@Repository public class UserDaoImpl implements UserDao

然后在您的应用中自动连接UserDaoImpl

- 编辑 -

要启用基于注释的转码,您必须添加

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

在应用程序上下文xml