Results 1 to 2 of 2

Thread: Could n't get session in DAO(Getting Null pointer Exception)

  1. #1

    Post Could n't get session in DAO(Getting Null pointer Exception)

    Hi

    I am trying to use Spring MVC and Hibernate in my project and have used Javax.peristence annotations to map the tables in entity classes to avoid table mapping in xml and have used all required configuration in common spring configuration xml file called "test-servlet.xml" . I am facing the problem that i could n't get session in my DAO class

    Code:
    import java.util.logging.Logger;
    
    import javax.annotation.Resource;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    @Transactional
    public class PlUserDao {
    	@Resource(name = "mySessionFactory")
    	private SessionFactory sessionFactory;
    	protected static Logger logger = Logger.getLogger("service");
    	
    	 public void setSessionFactory(SessionFactory sessionFactory) {
    	        this.sessionFactory = sessionFactory;
    	    }
    
    	// Stores a new guest:
    
    	public void add(Object plUser) {
    		 logger.fine("Adding new person");
    		Session session = this.sessionFactory.getCurrentSession();-Null Pointer Exception Over Here
    		session.save(plUser);
    	}
    
    }
    the below is the configuration file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		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" >
    
        <context:component-scan base-package="org.test.controller" />
    
        <annotation-driven />
    
        <resources
            location="/css/"
            mapping="/css/**" />
    
        <resources
            location="/js/libs/"
            mapping="/js/libs/**" />
    
        <resources
            location="/img/"
            mapping="/img/**" />
    
        <!-- Model -->
        <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    		<beans:property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    		<beans:property name="username" value="test" />
    		<beans:property name="password" value="test" />
    	</beans:bean>
    	
    
        <beans:bean id="mySessionFactory"    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
            <beans:property     name="dataSource" ref="dataSource" />
             <beans:property name="namingStrategy">
            <beans:bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
        </beans:property>
        
        
          <beans:property name="packagesToScan" value="com.purple.entity" />
           <beans:property name="hibernateProperties" >
                <beans:props>
                    <beans:prop key="hibernate.dialect" > org.hibernate.dialect.Oracle10gDialect</beans:prop>
                    <beans:prop key="hibernate.show_sql" >true</beans:prop>
                    <beans:prop key="hibernate.hbm2ddl.auto" >create</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>
    
        <!-- View -->
        <!-- **************************************************************** -->
        <!-- THYMELEAF-SPECIFIC ARTIFACTS -->
        <!-- TemplateResolver <- TemplateEngine <- ViewResolver -->
        <!-- **************************************************************** -->
    
        <beans:bean
            id="templateResolver"
            class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" >
    
            <beans:property
                name="prefix"
                value="/WEB-INF/pages/" />
    
            <beans:property
                name="suffix"
                value=".html" />
    
            <beans:property
                name="templateMode"
                value="HTML5" />
        </beans:bean>
    
        <beans:bean
            id="templateEngine"
            class="org.thymeleaf.spring3.SpringTemplateEngine" >
    
            <beans:property
                name="templateResolver"
                ref="templateResolver" />
        </beans:bean>
    
        <beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver" >
    
            <beans:property
                name="templateEngine"
                ref="templateEngine" />
        </beans:bean>
          <!--// View -->
    
    </beans:beans>
    Can anyone help me that my approach is right? why i am not getting Session?

    Thanks & Regards
    Vijay

  2. #2
    Join Date
    May 2011
    Location
    New Delhi, India
    Posts
    157

    Default

    Try using @Autowired instead of @Resource.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •