gpt4 book ai didi

java - getCurrentSession() - NullPointerException - 为什么?

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:23 25 4
gpt4 key购买 nike

我有一个问题,当我在 DAO 文件中调用 addCus() 方法时,我得到 NullPointerException。我尝试了很多解决方案,但仍然出现此错误。为什么 getCurrentSession() 方法 geterate 这个错误以及如何修复它??

pom.xml

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
</dependency>

my-servlet.xml

<mvc:annotation-driven />
<context:component-scan base-package="pl.classes"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


<context:property-placeholder location="mysql.properties" />

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="pl.classes.Customer" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>

<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>

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

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

CustomerDaoImpl

public class CustomerDatabaseDaoImpl implements CustomerDatabaseDao {

@Autowired
private SessionFactory session;

private Set<Customer> customers;
private String filename;
private File file;



@Transactional
public void addCus(Customer c) {
this.session.getCurrentSession().save(c);

}

堆栈跟踪

java.lang.NullPointerException
pl.dao.CustomerDatabaseDaoImpl.addCus(CustomerDatabaseDaoImpl.java:47)
pl.service.CustomerDatabaseServiceImpl.addCus(CustomerDatabaseServiceImpl.java:101)
pl.classes.CustomerDatabaseImpl.addCus(CustomerDatabaseImpl.java:76)
pl.controller.HomeController.customerList(HomeController.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:933)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:867)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

我的 sessionFactory 出了什么问题??

最佳答案

我假设 CustomerDatabaseDaoImpl 的第 47 行引用了 this.session.getCurrentSession().save(c);

Avoid the use of new to instantiate, just in case you are using it. Spring IOC will not work with new keyword. Hence this.session.getCurrentSession() will return null.

您尚未指定有关如何实例化 CustomerDatabaseDaoImpl 的详细信息。我正在向您解释解决此问题的正确方法。

@Autowired
private CustomerDatabaseDao customerDatabaseDao;

要使 spring 注入(inject)/ Autowiring CustomerDatabaseDao 的依赖项,它必须知道相同类型的 bin。从配置文件 (my-servlet.xml) 可以清楚地看出您没有定义这个 bean。因此使用 @Repository CustomerDatabaseDaoImpl 上的注释。 @Repository 只能用在具体类上How to use Spring @Repository, and @Service :

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

@Repository
public class CustomerDatabaseDaoImpl implements CustomerDatabaseDao {

@Autowired
private SessionFactory session;

@Transactional
public void addCus(Customer c) {
this.session.getCurrentSession().save(c);
}
}

下一个重要步骤是在配置文件中指定正确的包详细信息。特别是具体类的包。如果您有多个包,请使用“,”分隔值或仅提及父包:

<context:component-scan base-package="pl.classes,pl.dao"></context:component-scan>

您的服务类应该采用类似的方法。用 @Service 注释具体的服务类注释。使用 @Autowire 将服务类注入(inject)到 Spring Controller 中。在 <context:component-scan base-package="pl.classes,pl.dao,pi.service"></context:component-scan>. 中提及包裹

请按照本教程了解更多详细信息:HowToDoInJava并随时发表评论以获得进一步的帮助。

关于java - getCurrentSession() - NullPointerException - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625857/

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