GetCurrentSession () - NullPointerException - why?

I have a problem, when I call the addCus () method in the DAO file, I get a NullPointerException. I tried many solutions, but I still get this error. Why does the getCurrentSession () method geterate this error and how to fix it ??

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

}

Stack trace

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)

What happened to my sessionFactory?

+4
source share
2 answers

I assume that line number 47 CustomerDatabaseDaoImpl refers to this.session.getCurrentSession (). save (c);

, . Spring IOC . this.session.getCurrentSession() null.

, CustomerDatabaseDaoImpl. .

@Autowired
private CustomerDatabaseDao customerDatabaseDao;

Spring / CustomerDatabaseDao, . (my-servlet.xml) , bean. @Repository CustomerDatabaseDaoImpl. @Repository . Spring @Repository @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, Spring. <context:component-scan base-package="pl.classes,pl.dao,pi.service"></context:component-scan>.

, : HowToDoInJava .

+1

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

, @Autowired pl.classes. NullpointerException CustomerDatabaseDaoImpl pl.dao.

, Spring ?

, Spring, Spring. context:component-scan:

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

, Spring . ? @Autowired, Setter/Getter, Constructor ?

0

Source: https://habr.com/ru/post/1615192/


All Articles