gpt4 book ai didi

java - Spring AnnotationSessionFactoryBean NullPointerException

转载 作者:行者123 更新时间:2023-12-01 14:14:35 24 4
gpt4 key购买 nike

我是 Spring 的新手,正在尝试集成 Spring 和 Hibernate。但我在 SessionFactory 中面临空指针。

错误描述:

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)

我的dispatpar-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;

}

}

我的服务接口(interface)

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

我的 Controller 类从我进行这些调用的地方,我已经通过这个实例化了服务类

@Autowired
private UserService userservice;

在我调用的方法中

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

最佳答案

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

UserDaoImpl添加注解,并放在com.nptest包下

package com.nptest.dao;

@Repository public class UserDaoImpl implements UserDao

然后在您的应用程序中 Autowiring UserDaoImpl

--编辑--

要启用基于注释的交易,您必须添加

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

在应用程序上下文 xml 中

关于java - Spring AnnotationSessionFactoryBean NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com