2013-10-03 2 views
-1

sessionFactory에에서 NullPointerException이 해결하는 방법 내가봄 SessionFactory를

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
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/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="com.tsc.erp" /> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean id="transactionManager" 
class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
<property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
<property name="packagesToScan" value="com.tsc.erp.dao"/> 
<property name="dataSource" ref="dataSource" /> 
<property name="hibernateProperties"> 
<props> 
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
<prop key="hibernate.show_sql">false</prop> 

</props> 
</property> 
</bean> 

    <!-- <bean id="datasourcePropertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>classpath:datasource.properties</value> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders"> 
      <value>true</value> 
     </property> 
    </bean> --> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="refresh"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/tschrms" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 



<bean id="userdao" class="com.tsc.erp.dao.TscUserHome" > 
<property name="sessionFactory"><ref bean="sessionFactory"/></property> 

</bean> 


</beans> 

DAO 파일을 xml file

의 ApplicationContext 파일 내 봄에

package com.tsc.erp.dao; 

import java.util.List; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.springframework.beans.factory.annotation.Autowired; 


import org.springframework.stereotype.Repository; 

@Repository 
public class TscUserHome implements Tscuserdao{ 

    @Autowired 
    private SessionFactory sessionFactory; 



    public SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public void setSessionFactory(SessionFactory sessionFactory) { 

      this.sessionFactory = sessionFactory; 
    } 





    public List showEmployee() { 
      System.out.println(sessionFactory); 
      List emp = null; 
      Session session = sessionFactory.openSession(); 
     Transaction tx = null; 
      try{ 
      tx = session.beginTransaction(); 

     emp = session.createQuery("from tsc_user").list(); 

      tx.commit(); 
      } 

      catch (HibernateException e) { 
      if (tx!=null) 
       {tx.rollback(); 
      e.printStackTrace(); 
      } 

      } 
      catch(NullPointerException ne) 
      { 

       ne.getStackTrace(); 
      } 

      finally { 

      } 
      session.close(); 
      sessionFactory.close(); 
     return emp; 


     } 




} 

웹 SessionFactory를 구성한 내 DAO 파일 null입니다. XML 파일

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>esystem</display-name> 


    <filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
    </listener> 


    <welcome-file-list> 

    <welcome-file>loginpage.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

서비스 파일 (내 TscUserHome을 사용 중입니다)

패키지 com.tsc.erp.services;

import java.util.ArrayList; 
import java.util.List; 

import com.tsc.erp.dao.TscUserHome; 

public class Loginservice 
{ 
    private List parti_user = new ArrayList(); 
    private TscUserHome logindao=new TscUserHome(); 

    public void authentication() 
    { 
     try{ 
      System.out.println("first"); 
     parti_user =logindao.showEmployee(); 

     System.out.println(parti_user); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 




} 
+1

그게 우리가 제공하는 모든 정보입니다. 예를 들어 우리에게 줄 수 있겠습니까? 모름 ... 코드 샘플? –

+0

@KevinBowersox 무엇을 편집합니까? 나는 아직도 어떤 코드도 볼 수 없다. 또는 기타 특정 정보. 어떻게 도와야합니까? –

+0

코드를 제공하십시오. 스택 트레이스가 없습니다 – MayurB

답변

0

귀하의 DAO 빈에 sessionFactory 객체를 주입해야합니다. 예를 들어 모든

<bean id="MyDAO" class="com.mine.MyDAO"> 
<property name="sessionFactory" ref="id of your sessionFactory bean" /> 
</bean> 
+0

답장을 위해 thnks하지만 이미이 일을했다. –

0

첫째, 그렇지 않으면 PLS sessionFactory에와 DAO를 작성하여 XML을 공유, 당신은 DAO에서 sessionFactory에의 getter 메소드 만든 있는지 확인합니다.

+0

나는 이미 그것을 공유하고있다. –

관련 문제