vendredi 10 avril 2015

SessionFactory NullPointerException - Spring Hibernate

I am beginner in Spring and I am trying to use Spring with an external properties file.


I encountered a NullpointerException for my sessionFactory


This in my applicationContext.xml :



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
http://ift.tt/OGfeU2 http://ift.tt/KC395X
http://ift.tt/GArMu7 http://ift.tt/1ldEMZY">

<context:property-placeholder location="classpath:database.properties" />
<context:component-scan base-package="com.erm.monitoring" />

<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>

<bean
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="connection.provider_class">${connection.provider_class}</prop>
<prop key="hibernate.c3p0.acquire_increment">${hibernate.c3p0.acquire_increment}</prop>
<prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.acquireRetryAttempts">${hibernate.c3p0.acquireRetryAttempts}</prop>
<prop key="hibernate.c3p0.acquireRetryDelay">${hibernate.c3p0.acquireRetryDelay}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
<prop key="hibernate.query.factory_class">${hibernate.query.factory_class}</prop>
</props>
</property>
</bean>

<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
id="hibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


Here is my database.properties :



database.driver=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:@X:1521:X
database.user=X
database.password=X

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.idle_test_period=60
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=2
hibernate.c3p0.max_statements=50
hibernate.c3p0.timeout=0
hibernate.c3p0.acquireRetryAttempts=1
hibernate.c3p0.acquireRetryDelay=250
hibernate.show_sql=true
hibernate.current_session_context_class=thread
hibernate.query.factory_class=org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory


Finally this is my helper class :



public class MonitoringHelper implements Serializable {

private static final long serialVersionUID = 1L;

@Autowired
SessionFactory sessionFactory;

Session session;

public MonitoringHelper() {
}

public List getSourceApplication() {
List<String> sourceList = null;
List<SelectItem> sourceSelectItem = null;
String query = "X";
try {
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
sourceList = session.createSQLQuery(query).list();
sourceSelectItem = this.convertStringToSelectItem(sourceList);
} catch (Exception e) {
e.printStackTrace();
}
return sourceSelectItem;
}


Could you tell me why my sessionFactory is null knowing that I used an autowired on it ?


Thanks


Aucun commentaire:

Enregistrer un commentaire