Joined: Thu Nov 14, 2013 11:00 am Posts: 4
|
I am developing a Struts 2-Spring 3-Hibernate 4 integrated web application.Whenever I run the application on web server it is always giving Null Pointer exception because of Session Factory being null. Please suggest me some solution as I am stuck in this problem for a long time. I have placed spring configuration file,Dao Implementation class and web.xml as follows:
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/databaseConfig.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.databaseurl}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="packagesToScan" value="in.nic.eservicebook.pojo" /> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>in.nic.eservicebook.pojo.Title</value> <value>in.nic.eservicebook.pojo.BloodGroup</value> <value>in.nic.eservicebook.pojo.Community</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean>
<bean id="MasterDao" class="in.nic.eservicebook.dao.MasterDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
</beans>
MasterDaoImpl.java
package in.nic.eservicebook.dao;
import in.nic.eservicebook.pojo.BloodGroup; import in.nic.eservicebook.pojo.Community; import in.nic.eservicebook.pojo.Title; import in.nic.util.HibernateUtil; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;
public class MasterDaoImpl implements MasterDao { SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; }
public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }
@SuppressWarnings("unchecked") public List<Title> listTitle() { List<Title> titleList = null; Session session=null;
try { session = getSessionFactory().getCurrentSession(); session.beginTransaction(); titleList=(List<Title>)session.createQuery("from Title").list(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback();
} finally { session.close(); } return titleList; } @SuppressWarnings("unchecked") public List<BloodGroup> listBloodGroup() { List<BloodGroup> bloodGroupList = null; Session session = getSessionFactory().openSession(); session.beginTransaction(); try {
bloodGroupList=(List<BloodGroup>)session.createQuery("from BloodGroup").list(); session.getTransaction().commit();
} catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback();
} finally { session.close(); } return bloodGroupList; } @SuppressWarnings("unchecked") public List<Community> listCommunity() { List<Community> communityList = null; Session session = getSessionFactory().openSession(); session.beginTransaction(); try {
communityList=(List<Community>)session.createQuery("from Community").list(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback();
} finally { session.close(); } return communityList; }
}
web.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>esb</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-config.xml </param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </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>eservicebook/index.jsp</welcome-file> </welcome-file-list> </web-app>
|
|