Bonjour à tous,

Je souhaiterai implémenter une BDD en utilisant les technos Hibernate, Spring, java à mon application et je coince depuis pas mal de temps.

Je souhaite dans un premier temps afficher les données de ma BDD dans mon appli. Mais lors de l'execution de l'appli, j'obtiens une erreur NPE sur ma méthode hibernateTemplate et je ne vois pas comment résoudre ce pbl.

Ci-dessous mon DAO:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MobileDAO implements IMobileDAO{
 
    private HibernateTemplate hibernateTemplate;
 
    public void setSessionFactory(SessionFactory sessionFactory) {
        hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
 
    /**
     * Get List of mobiles from database
     * @return list of all mobiles
     */
 
 
    @SuppressWarnings("unchecked")
    @Override
    public List<Mobile> getMobiles(){
        return hibernateTemplate.find("from Mobile");
        //return hibernateTemplate.loadAll(Mobile.class);
 
    }
Mon fichier de config de la BDD:
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
<!-- Database mapping -->
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/>
        <property name="username" value="XXX"/>
        <property name="password" value="XXX"/>
      </bean>
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="mappingResources">
            <list>
                <value>/fr/cls/commons/mapping/SatelliteTerminalTest.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>hibernate.dialect=org.hibernate.dialect.OracleDialect</value>
        </property>
    </bean>
 
    <bean id="hibernateTemplate"  class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    <bean id="MobileDAO" class="fr.cls.commons.dao.MobileDAO">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
 
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
</beans>


mon fichier demo-servlet.xml:

Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
    <!-- the application context definition for the lrit DispatcherServlet -->
 
    <!-- Configures Hibernate - Database Config -->
    <import resource="/spring/db-config.xml"/>
    <bean id="MobileCRUDServiceV2" class="fr.cls.commons.spring.test.mobile.MobileCRUDServiceV2"></bean>
 
 
    <!-- application mapping -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /Mobile.htm=manageMobileControler
                /UserTerminal.htm=manageUserTerminalControler
                /Vessel.htm=manageVesselControler
            </value>           
        </property>
    </bean>
 
    <!-- AOP configuration -->
    <aop:config>
        <aop:pointcut id="applicationSecurity"
            expression="execution(* org.springframework.web.servlet.mvc.Controller.*(..))" />
    </aop:config>
 
    <!-- Actions -->
    <bean id="MethodNameResolver" class="fr.cls.commons.spring.controller.multiaction.PrefixedParameterMethodNameResolver">
        <property name="defaultMethodName"><value>defaultAction</value></property>
        <property name="paramName"><value>jsAction</value></property>
    </bean>
 
    <bean id="manageMobileControler" class="fr.cls.commons.spring.test.mobile.MobileControler">
        <property name="methodNameResolver">
            <ref local="MethodNameResolver"/>
        </property>
        <property name="commandName"                 value="fr.cls.commons.spring.test.mobile.MobileCommand" />
        <property name="commandAttrName"             value="mobileCommand" />
        <property name="crudService"                  ref="MobileCRUDServiceV2"/>
    </bean>
</beans>


et ma classe qui appelle la méthode MobileDAO.getMobiles:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MobileCRUDServiceV2 implements CRUDService<Mobile, Integer>{
 
    private MobileDAO mobileDAO;
    private Util util;
    private SessionFactory sessionFactory;
     private Map<Integer,Mobile> allMap = new HashMap<Integer,Mobile>();
 
 
    @Override
    // @Transactional
    public List<Mobile> findAll() throws ServiceException {
        // TODO Auto-generated method stub
        //return mobileDAO.getMobiles();
        //return sessionFactory.getCurrentSession().createQuery("from SATELLITE_TERMINAL_TEST").list();
        //return null;    
        //MobileDAO mobileDAO = new MobileDAO();
        List<Mobile> mobile=mobileDAO.getMobiles();
        return mobile;
        //return new ArrayList<Mobile>(mobileDAO.getMobiles());
        //return null;
    }
erreur obtenue: java.lang.NullPointerException
at fr.cls.commons.spring.test.mobile.MobileCRUDServiceV2.findAll(MobileCRUDServiceV2.java:40)
Pouvez-vous m'aider sur mon problème de nullpointerexception pour ma méthode mobileDAO.getMobiles(), je suis à court d'idées...

En vous remerciant par avance.