IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

l'injection de dépendance de la fabrique d' entityManager ne se fait pas


Sujet :

Spring Java

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    958
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 958
    Points : 213
    Points
    213
    Par défaut l'injection de dépendance de la fabrique d' entityManager ne se fait pas
    bonjour,

    je tombe sur l'erreur suivante lors du test du projet : java.lang.NullPointerException sur la ligne du findAll du fichier ci-dessous : cela signifie que l'injection de dépendance ne se fait pas, et plus particulièrement l'injection dans la fabrique de gestionnaire d'entité (EntityManagerFactory).
    je peux dire cela au vu des lignes concernées par l'erreur.

    pourquoi cette erreur?

    voici la classe de test:

    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
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    public class InitDB {
     
        private static IEmployeDao employeDao=null;
        private static ICotisationDao cotisationDao=null;
        private static IIndemniteDao indemniteDao=null;
     
        @BeforeClass
        public static void init(){
     
            ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config-dao.xml");
     
            employeDao =(IEmployeDao) ctx.getBean("employeDao");
            cotisationDao=(ICotisationDao) ctx.getBean("cotisationDao");
            indemniteDao=(IIndemniteDao) ctx.getBean("indemniteDao");
     
     
        }
     
        @Test
        public void initDB(){
     
            for (Cotisation cotisation : cotisationDao.findAll()){
                cotisationDao.destroy(cotisation);
            }
            //remplissage
            Cotisation cot=new Cotisation(1.2, 1.3, 3.1, 3.2);
            cotisationDao.create(cot);
     
            //affichage du contenu
            for (Cotisation cot2:cotisationDao.findAll()){
                System.out.println(cot2);
            }
     
     
        }
     
     
    }
    vioci la classe de 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
    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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
     
    public class CotisationDao implements ICotisationDao{
     
     
        public EntityManagerFactory entityManagerFactory;
        public EntityManager em;
     
        public void setEntityManagerFactory(EntityManagerFactory emf) {
            this.entityManagerFactory = emf;
        }
     
        public CotisationDao() {
        }
     
     
     
     
        public Cotisation create(Cotisation employe) {
     
            em=entityManagerFactory.createEntityManager();
            try{
                em.persist(employe);
            }catch (Exception e){
                throw new PamException("erreur dans CotisationDao - create / "+e.getMessage(), 1);
            }
            em.close();
            return employe;
     
        }
     
        public Cotisation edit(Cotisation employe) {
     
            em=entityManagerFactory.createEntityManager();
            try{
                employe=em.merge(employe);
            }catch (Exception e){
                throw new PamException("erreur dans CotisationDao - edit / "+e.getMessage(), 1);
            }
            em.close();
            return employe;
     
        }
     
        public void destroy(Cotisation employe) {
     
            em=entityManagerFactory.createEntityManager();
            try{
                em.remove(employe);
                em.close();
            }catch (Exception e){
                throw new PamException("erreur dans CotisationDao - destroy / "+e.getMessage(), 1);
            }
     
     
        }
     
        public Cotisation find(Long id) {
     
            em=entityManagerFactory.createEntityManager();
            Cotisation employe=em.find(Cotisation.class, id);
            if (employe==null)
                throw new PamException("erreur dans CotisationDao - find / aucune Cotisation d'id "+id,1);
            em.close();
            return employe;
        }
     
        public List<Cotisation> findAll() {
     
            em=entityManagerFactory.createEntityManager();
            List<Cotisation> liste=em.createQuery("select c from Cotisation c").getResultList();
            em.close();
            return liste;
     
     
        }
     
    }


    et voici le fichier de configuration des beans de spring:

    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
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    <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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     
        <bean id="employeDao" class="dao.EmployeDao">
            <property name="entityManagerFactory" ref="entityManagerFactory">
     
            </property>
        </bean>
        <bean id="cotisationDao" class="dao.CotisationDao">
            <property name="entityManagerFactory" ref="entityManagerFactory">
     
            </property>
     
        </bean>
     
        <bean id="indemniteDao" class="dao.IndemniteDao">
            <property name="entityManagerFactory" ref="entityManagerFactory">
     
            </property>
     
        </bean>
     
        <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
     
            <property name="persistenceUnitName" value="javaee-1PU"></property>
     
     
        </bean>
     
    </beans>
    et le persistence.xml:

    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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
      <persistence-unit name="javaee-1PU" transaction-type="RESOURCE_LOCAL">
        <!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
        <class>jpa.Employe</class>
        <class>jpa.Cotisation</class>
        <class>jpa.Indemnite</class>
        <properties>
          <property name="hibernate.connection.username" value="root"/>
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
          <property name="hibernate.connection.password" value="password"/>
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jbossdb"/>
          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
      </persistence-unit>
    </persistence>
    je ne mets pas le fichier des entités (une clé primaire, quelques attributs, des constructeurs, getters, setters toString et hashCode).

    voici par contre et pour finir la liste des jars de la bibliothèque:

    voir la photo.

    merci pour toute aide,

    olivier.

  2. #2
    Membre expérimenté
    Avatar de Patriarch24
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2003
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2003
    Messages : 1 047
    Points : 1 640
    Points
    1 640
    Par défaut
    Il faudrait que tu postes la trace de l'exception pour qu'on puisse y voir plus clair.
    En premier lieu, utilisez un moteur de recherche.
    En second lieu, postez sur le forum adéquat !

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    958
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 958
    Points : 213
    Points
    213
    Par défaut
    hop, la voici :

    Testcase: initDB(dao.InitDB): Caused an ERROR
    null
    java.lang.NullPointerException
    at dao.CotisationDao.create(CotisationDao.java:45)
    at dao.InitDB.initDB(InitDB.java:45)


    Test dao.InitDB FAILED
    ...mais j'ai peur que cela ne serve pas à grand chose, vu qu'elle est toute petite.
    pour infos:
    CotisationDao, ligne 45:"em=entityManagerFactory.createEntityManager();"
    initDB, ligne 45 :"cotisationDao.create(cot);"

    olivier.

  4. #4
    Membre expérimenté
    Avatar de Patriarch24
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2003
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2003
    Messages : 1 047
    Points : 1 640
    Points
    1 640
    Par défaut
    Pourquoi n'injectes-tu pas directement l'entity manager ?
    En premier lieu, utilisez un moteur de recherche.
    En second lieu, postez sur le forum adéquat !

  5. #5
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    958
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 958
    Points : 213
    Points
    213
    Par défaut
    eh bien oui, je pourrais, mais il faut que je précise que je lis un bouquin sur spring et que l'approche est progressive... l'injection d'entitymanager n'est vue qu'en dernier dans le chapitre sur le mapping objet-relationnel.

    mais je vais essayer.

    réponse bientôt!

    olivier.

    ps:merci!

Discussions similaires

  1. [EJB3] [JBoss] Injection de dépendance circulaire ?
    Par Claythest dans le forum Java EE
    Réponses: 6
    Dernier message: 04/08/2009, 08h11
  2. [Framework] Injection de dépendances ; comment se passer du XML ?
    Par ummon99 dans le forum Spring
    Réponses: 3
    Dernier message: 12/01/2008, 09h19
  3. [EJB3] Injection de dépendance et Stateful
    Par newbeewan dans le forum Java EE
    Réponses: 1
    Dernier message: 15/05/2007, 07h33
  4. [Integration] [EasyMock] Injection de dépendance à l'éxécution
    Par frangin2003 dans le forum Spring
    Réponses: 2
    Dernier message: 06/03/2007, 11h06
  5. Spring + TagSupport et injection de dépendance
    Par worldchampion57 dans le forum Spring Web
    Réponses: 2
    Dernier message: 26/02/2007, 09h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo