내 DaoClasshibernatetemplate.getSessionFactory()는 NullPointerException이
@Repository("genObj")
public class GeneralQueries {
HibernateTemplate hibernatetemplate;
public HibernateTemplate getHibernatetemplate() {
return hibernatetemplate;
}
public void setHibernatetemplate(HibernateTemplate hibernatetemplate) {
this.hibernatetemplate = hibernatetemplate;
}
public String getStringfromQuery(String sql)
{
SessionFactory sessionFactory=hibernatetemplate.getSessionFactory();
Session session=sessionFactory.openSession();
String data=null;
try
{
System.out.println(sql);
data=session.createSQLQuery(sql).list().toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return data;
}}
이 방법 문자열로 데이터를 반환
내 컨트롤러 클래스를 던졌습니다
@Controller
public class SchoolStudentsConfirmationContrl
{
@Autowired
SchoolStudentsConfirmationIntr schoolstdconfirmservice;
@Autowired
GeneralQueries genObj=new GeneralQueries();
@RequestMapping(value="/getData",method=RequestMethod.GET)
public ModelAndView getData(@ModelAttribute("schooldetailsform")SchoolDetailsForm formbean,HttpServletRequest request)
{
String PageHeading = "";
try
{
String district = request.getSession().getAttribute("dist_code").toString();
PageHeading = "BAS Students Confirmation for the Academic Year:"+ formbean.getAc_year() + " <br> District:"
+ genObj.getStringfromQuery("select dist_name from pmss_districts_mst where dist_code=" + district + "")+"";
mav.setViewName("showreportwithmenu");
}
catch(Exception e)
{
e.printStackTrace();
}
return mav;
}
}
인스턴트 메신저 genObj.getStringfromQuery을 (전화를 시도하는) 방법 하지만 줄 null 포인터 예외를 throw합니다. SessionFactory sessionFactory = hibernatetemplate.getSessionFactory();
내 설정 파일
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" 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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="cgg.gov.in.*" annotation-config="true"/>
<bean id="tiles" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles3.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://x.x.x.x/test" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
</bean>
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" name="sessionFactory">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.autocommit">false</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>cgg.gov.in.model.login.LoginForm</value>
</list>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateTemplate" name="hibernatetemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
</beans>
기본적으로 자동 배선이 작동하지 않게하는 새로운 'GeneralQueries()'를하고 있으며 스프링 관리 인스턴스를 사용합니다. 그 다음에 당신의 dao에있는'HIbernateTemplate' 필드에'@ Autowired'가 없습니다. 또한 코드에 결함이 있습니다. 이상한 문제가 발생하지 않는 한'openSession'을 사용하지 마십시오.'openSession'은 새로운 세션을 열고 필요하면'Connection'을 엽니 다. 연결을 제대로 닫지 않았으므로 누수 및 메모리 누수로 시작). –
또한'Session'과'SessionFactory'로 작업 할 때 나는'HibernateTemplate'을 사용하지 않고'SessionFactory'를 직접 주입하는 것을 제안합니다. –