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 :

Problème avec Spring et le bean SessionFactory


Sujet :

Spring Java

  1. #1
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut Problème avec Spring et le bean SessionFactory
    Bonsoir,
    Je réalise actuellement une application web qui utilise Spring Hibernate JSF et MySQL ...
    Lorsque je veut lancer mon application depuis NetBeans 7.3.1 j'ai le message "SEVERE" suivant :

    SEVERE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tazaSessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
    Voici mon applicationContext.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
    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
     
    <?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:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p" 
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     
     
        <!-- Data source access-->
        <bean id="tazaDataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/Taza"/>
            <property name="username" value="root"/>
            <property name="password" value="007micke"/>
        </bean>
     
     
        <bean id="tazaSessionFactory" 
                  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
                  p:dataSource-ref="tazaDataSource" p:packagesToScan="fr.itldev.model"> 
            <property name="hibernateProperties"> 
                <value> 
                    hibernate.format_sql=true 
                    hibernate.dialect=org.hibernate.dialect.MySQLDialect 
                    hibernate.hbm2ddl.auto=create 
                </value> 
            </property> 
        </bean> 
     
        <bean id="transactionManager"
              class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="tazaSessionFactory"/>
        </bean>
     
        <tx:annotation-driven transaction-manager="transactionManager" />
     
     
        <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
        <context:component-scan base-package="fr.itldev.tazaWebapp"/>
     
    </beans>
    Voilà pour le moment, merci d'avance pour toute l'aide.

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Bonjour,
    Utilise la version 3.1.

    Essaie avec

    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create</prop> 
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
    </property>

    A+.

  3. #3
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    Bonjour,
    Et merci andry.aime pour ta réponse.
    j'ai fais les modification dans le bean ainsi que dans "xsi:schemaLocation" mais le problème persiste.

    J'ai finalement choisi le système D (comme dégonflé), j'ai repris le fichier applicationContext.xml fournis par mon tuteur lors de mon stage et j'ai supprimé tout ce qui été lié aux applications internes.

    çà donne çà :
    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
     
    <?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:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p" 
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
     
            <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/taza" />
        </bean>
     
         <!--Gestionnaire de persistence Unit--> 
        <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"
              p:persistenceXmlLocations="classpath:persistence.xml"
              p:defaultDataSource-ref="dataSource" />
     
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource">
            <property name="persistenceUnitManager" ref="pum"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                      p:databasePlatform="org.hibernate.dialect.MySQL5InnoDBDialect" p:showSql="true">
                </bean>
            </property>
        </bean>
     
         <!--le gestionnaire de transactions--> 
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory" p:validateExistingTransaction="true">
        </bean>
     
        <context:annotation-config />
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
     
         <!--annotations de persistance--> 
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
     
         <!--traduction des exceptions--> 
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     
        <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
        <context:component-scan base-package="fr.itldev.tazaWebapp"/>
     
    </beans>
    Maintenant il faut que je trouve comment expliquer ce micmac sans avoir l'air d'un pro du système D.

    Donc si quelq'un à une idée sur la façon de présenter çà je suis preneur et il aura toute ma gratitude pour avoir contribué à l'obtention de mon titre de développeur.

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 483
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 483
    Par défaut
    tu veux dire que tu ne comprends pas ce que fait ton xml?

  5. #5
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 483
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 483
    Par défaut
    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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    <?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:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p" 
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <!-- On importe différent namespace qui correspondent à différentes fonctionnalités de spring dont on a besoin. xml standard--> 
     
            <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/taza" />
        </bean>
    <!-- on définit un bean appelé "dataSource" qui est récupéré depuis le jndi du conteneur à l'endroit indiqué -->
     
         <!--Gestionnaire de persistence Unit--> 
        <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"
              p:persistenceXmlLocations="classpath:persistence.xml"
              p:defaultDataSource-ref="dataSource" />
    <!-- on définit un persistence unit appelé pum, de type jpa et configuré à partir du persistence.xml. On lui passe aussi en paramètre la datasource à utiliser -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource">
            <property name="persistenceUnitManager" ref="pum"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                      p:databasePlatform="org.hibernate.dialect.MySQL5InnoDBDialect" p:showSql="true">
                </bean>
            </property>
        </bean>
    <!-- on définit un entity manager spring, de type hibernate, et utilisant le persistence unit mentionn -->
     
         <!--le gestionnaire de transactions--> 
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory" p:validateExistingTransaction="true">
        </bean>
    <!-- on en récupère le transaction manager -->
     
        <context:annotation-config />
    <!-- on dit à spring qu'il faut tenir compte des annotations spring -->
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
    <!-- et on précise comment gérer les annotations de transaction -->
     
         <!--annotations de persistance--> 
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <!-- un bean qui doit probablement être injecté quelque part -->
     
         <!--traduction des exceptions--> 
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     
    <!-- un bean qui doit probablement être injecté quelque part -->
     
        <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
        <context:component-scan base-package="fr.itldev.tazaWebapp"/>
    <!-- les packages à scanner pour les annotations -->
    </beans>

  6. #6
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    Bonjour tchize_,
    Et 1000 pour ta réponse.
    Du coup je me sens un peut crétin en relisant mon message. Car même si je débute en Spring (disons même dans le développement), je me rends compte qu'avec le ventre plein tout paré plus clair, aussi çà faisait depuis 5 jours en non-stop (sauf pour dormir) que j'étais sur ce machin et mon ami "le moteur de recherche qui commence par G".
    Et tout à l'heure j'étais un peut comme çà


    Merci encore à toi et à andry.aime.

  7. #7
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    Bonjour à tous,
    Je pensez avoir résolut mon problème mais finalement non le nouveau applicationContext.xml que j'avais mis en place ne va toujours pas mais au moins je n'est plus les même messages d'erreur.

    J'ai maintenant le message suivant :
    SEVERE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataMaquette': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: fr.itldev.service.TazaServiceImpl fr.itldev.tazaWebapp.data.DataMaquette.tazaService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [fr.itldev.service.TazaServiceImpl] is defined: Unsatisfied dependency of type [class fr.itldev.service.TazaServiceImpl]: expected at least 1 matching bean
    et voici le début de la classe citée dans le log + une des utilisation de TazaService :
    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
     
    @Service
    public class DataMaquette implements Serializable{
     
        private org.apache.log4j.Logger logger=org.apache.log4j.Logger.getLogger(this.getClass());
        private Projet projetCourant;
        private Projet projet1;
        private Projet projet2;
        private DerCommit commit1;
        private DerCommit commit2;
        private HistoBuild build;
        public List<Projet> projets;
        private List<HistoBuild> builds;
        private String login;
     
        @Autowired
        TazaServiceImpl tazaService;
        @PersistenceContext
        protected EntityManager entityManager;
     
    //  ........
        public List<Projet> getProjets() {
            this.projets = new ArrayList<Projet>();
            try {
                projets = tazaService.listerProjets();
            } catch (Exception e) {
                logger.error("Erreur de listing des projets : " + e.toString());
            }
            return projets;
        }
    Je ne sais pas si çà peut être aussi la cause du problème mais j'ai aussi annoté TazaServiceImp avec @Service (c'est ma classe service dans le package TazaSource).

    Je peux fournir plus de détail sur demande.

    Merci d'avance pour vos réponses.

  8. #8
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 483
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 483
    Par défaut
    Tu as essayé d'injecter un bean de type fr.itldev.service.TazaServiceImpl mais Spring ne trouve rien qui corresponde dans sa config ou ses classes annotées:
    No unique bean of type [fr.itldev.service.TazaServiceImpl]
    => Tu peux nous montrer ton
    fr.itldev.service.TazaServiceImpl

    aussi, est-ce que fr.itldev.service.* fait bien partie des packages scannés par spring?

  9. #9
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    Merci pour ta réponse tchize_

    En fait je viens d'avoir presque la même info par mon ex-tuteur de stage.

    De ce que j'ai compris, lorsque qu'on fait de l'Autowiring sur une classe qui n'est pas présente dans le même package, il faut le dire à Spring (façon de parler).

    je viens de rajouter çà dans mon applicationContext.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <bean id="tazaService" class="fr.itldev.service.TazaServiceImpl"/>
    et effectivement j'ai plus le même message qu'avant mais cette fois c'est l'Autowired dans TazaServiceImpl pour ProjetDAOImpl qui ce trouve dans un autre dossier du package TazaSource.

    donc je pense qu'il va falloir le faire pour toutes mes classes de DAO?

    Est ce qu'il n'y a pas un moyen de le faire en un seul bean?

  10. #10
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    Bon, je viens de le faire pour mes DAO mais çà ne veut pas passer.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
        <bean id="projetDao" class="fr.itldev.jpadao.ProjetDaoImpl" />
        <bean id="derCommitDao" class="fr.itldev.jpadao.DerCommitDaoImpl" />
        <bean id="histoBuildDao" class="fr.itldev.jpadao.HistoBuildDaoImpl" />
    j'ai cette fois le message suivant :

    SEVERE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tazaService': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fr.itldev.tazaInterface.ProjetDao fr.itldev.service.TazaServiceImpl.projetDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projetDao': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void fr.itldev.jpadao.ProjetDaoImpl.setSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: Unsatisfied dependency of type [interface org.hibernate.SessionFactory]: expected at least 1 matching bean
    voici le code de la classe TazaServiceImpl :

    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
     
     
    package fr.itldev.service;
     
    import fr.itldev.model.DerCommit;
    import fr.itldev.model.HistoBuild;
    import fr.itldev.model.Projet;
    import fr.itldev.tazaInterface.DerCommitDao;
    import fr.itldev.tazaInterface.HistoBuildDao;
    import fr.itldev.tazaInterface.ProjetDao;
    import fr.itldev.tazaInterface.TazaService;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
     
    /**
     *
     * @author itldev
     */
    @Service
    @Transactional(readOnly = true)
    public class TazaServiceImpl implements TazaService{
     
        @Autowired
        private ProjetDao projetDao;
        @Autowired
        private HistoBuildDao histoBuildDao;
        @Autowired
        private DerCommitDao commitDao;
     
        public void setProjetDao(ProjetDao projetDao) {
            this.projetDao = projetDao;
        }
     
        public void setHistoBuildDao(HistoBuildDao histoBuildDao) {
            this.histoBuildDao = histoBuildDao;
        }
     
        public void setCommitDao(DerCommitDao commitDao) {
            this.commitDao = commitDao;
        }
     
        @Transactional(readOnly = false)
        @Override
        public void ajouterProjet(Projet p) {
            projetDao.ajouterProjet(p);
        }
     
        @Transactional(readOnly = false)
        @Override
        public void modifierProjet(Projet p) {
            projetDao.modifierProjet(p);
        }
     
        @Transactional(readOnly = false)
        @Override
        public void supprinerProjet(Projet p) {
            projetDao.supprinerProjet(p);
        }
     
     
        @Override
        public Projet rechercherProjet(String nomProjet) {
            return projetDao.rechercherProjet(nomProjet);
        }
     
        @Override
        public List<Projet> listerProjets() {
            List<Projet> projets = projetDao.listerProjets();
            return projets;
        }
        //........
    }
    et pour ProjetDaoImpl :

    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
     
    package fr.itldev.jpadao;
     
    import fr.itldev.model.Projet;
    import fr.itldev.tazaInterface.ProjetDao;
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.hibernate.SessionFactory;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
     
    /**
     *
     * @author itldev
     */
    @Transactional(propagation = Propagation.MANDATORY)
    @Repository("projetDao")
    public class ProjetDaoImpl implements ProjetDao{
        private SessionFactory sessionFactory;
     
        @Autowired
        public void setSessionFactory(SessionFactory factory){
            sessionFactory=factory;
        }
     
        @Override
        public void ajouterProjet(Projet p){
            sessionFactory.getCurrentSession().save(p);
        }
     
        @Override
        public void modifierProjet(Projet p) {
            sessionFactory.getCurrentSession().merge(p);
        }
     
        @Override
        public void supprinerProjet(Projet p) {
            sessionFactory.getCurrentSession().delete(p);
        }
     
        @Override
        public Projet rechercherProjet(String nomProjet) {
            Projet p = (Projet) sessionFactory.getCurrentSession().createCriteria(Projet.class, nomProjet).uniqueResult();
     
            return p;
        }
     
        @Override
        public List<Projet> listerProjets() {
            ArrayList<Projet> projets;
            projets = (ArrayList<Projet>) sessionFactory.getCurrentSession().createCriteria(Projet.class).list();
     
            return projets;
        }
     
    }

  11. #11
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Bonjour,

    Il faut lire les messages d'erreur
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: Unsatisfied dependency of type [interface org.hibernate.SessionFactory]: expected at least 1 matching bean
    tu ne trouves pas de similitude avec l'ancien message?
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [fr.itldev.service.TazaServiceImpl] is defined: Unsatisfied dependency of type [class fr.itldev.service.TazaServiceImpl]: expected at least 1 matching bean
    Le bean sessionFactory doit être lié avec les configurations de hibernate. Fait une recherche sur les cours de spring, tu trouveras d'exemples.

    A+.

  12. #12
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    bonjour et merci pour ta réponse.

    donc je doit remettre le bean sessionFactory que j'avais au départ mais je me retrouve avec le même problème du début.

    c'est à dire :
    Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
    voici mon sessionFactory :
    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
     
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="annotatedClasses">
                <list>
                    <value>fr.itldev.model.DerCommit</value>
                    <value>fr.itldev.model.HistoBuild</value>
                    <value>fr.itldev.model.Projet</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">false</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                </props>
            </property>
        </bean>

  13. #13
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Bonjour,

    Quelles sont les versions de jar que tu utilises? Si tu utilises maven, tu peux nous montrer ton pom?

    A+.

  14. #14
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    bonjour,

    Voici mon pom.xml mais bon j'ai fait tellement de modifications depuis le début de ce projet que je ne sais plus vraiment ce que j'utilise et ce que je n'utilise pas.

    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
     
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>fr.itldev</groupId>
        <artifactId>TazaWebapp</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
     
        <name>TazaWebapp</name>
     
        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
     
        <repositories>
            <repository>  
                <id>prime-repo</id>  
                <name>PrimeFaces Maven Repository</name>  
                <url>http://repository.primefaces.org</url>  
                <layout>default</layout>  
            </repository>
        </repositories>
     
        <dependencies>
            <dependency>
                <groupId>fr.itldev</groupId>
                <artifactId>TazaSource</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish</groupId>
                <artifactId>javax.faces</artifactId>
                <version>2.0.11</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
                <version>3.5</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3</version>
            </dependency>
     
            <!-- Spring framework -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>3.2.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>3.2.0.RELEASE</version>
            </dependency>
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-orm</artifactId> 
                <version>3.2.0.RELEASE</version> 
            </dependency>
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-test</artifactId> 
                <version>3.2.0.RELEASE</version> 
            </dependency> 
     
            <!-- Hibernate -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>3.3.2.GA</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate</artifactId>
                <version>3.2.5.ga</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>ejb3-persistence</artifactId>
                <version>1.0.1.GA</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.2.0.CR1</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-c3p0</artifactId>
                <version>4.2.0.CR1</version>
            </dependency>
     
            <!-- MySQL-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.26</version>
            </dependency>
     
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
     
            <!--Autres Tests-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.16</version>
                <scope>provided</scope>
            </dependency>
            <dependency> 
                <groupId>junit</groupId> 
                <artifactId>junit</artifactId> 
                <version>4.10</version>
                <type>jar</type>
            </dependency>
     
        </dependencies>
     
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>6.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    Par contre j'ai réussi à trouver un tuto qui se rapproche grandement de ce que j'ai mis en place (hibernate-jpa-spring-tapestry) mis à part que moi je n'utilise pas tapestry.

    J'ai modifié mais DAO en remplaçant le SessionFactory par l'EntityManager.

    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
     
    package fr.itldev.jpadao;
     
    import fr.itldev.model.Projet;
    import fr.itldev.tazaInterface.ProjetDao;
    import java.util.ArrayList;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
     
    /**
     *
     * @author itldev
     */
    @Transactional(propagation = Propagation.MANDATORY)
    @Repository("projetDao")
    public class ProjetDaoImpl implements ProjetDao{
        @PersistenceContext
        private EntityManager entityManager;
     
     
        @Override
        public void ajouterProjet(Projet p){
            entityManager.persist(p);
        }
     
        @Override
        public void modifierProjet(Projet p) {
            entityManager.merge(p);
        }
     
        @Override
        public void supprinerProjet(Projet p) {
            entityManager.remove(p);
        }
     
        @Override
        public Projet rechercherProjet(String nomProjet) {
            Projet p = (Projet) entityManager.find(Projet.class, nomProjet);
     
            return p;
        }
     
        @Override
        public List<Projet> listerProjets() {
            ArrayList<Projet> projets;
            projets = (ArrayList<Projet>) entityManager.createQuery("select p from Projet p");
     
            return projets;
        }
     
     
    }
    J'arrive maintenant à afficher les pages web mais je n'est pas de données dedans et plus aucun messages d'alerte donc je vais mettre quelque "try catch" et je vous tiens informé.

  15. #15
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Bonjour,

    Tu dois avoir d'erreur à cette ligne,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    projets = (ArrayList<Projet>) entityManager.createQuery("select p from Projet p");
    createQuery retourne un type Query or que projets est de type List. Tu dois aussi l'exécuté pour récupérer le résultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    projets = (ArrayList<Projet>) entityManager.createQuery("select p from Projet p").getResultList();
    A+

  16. #16
    Membre averti
    Homme Profil pro
    Développeur Java Débutant
    Inscrit en
    Juin 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java Débutant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 22
    Par défaut
    Salut andry,
    Merci, j'avais vu ma petite boulette, j'ai corrigé et pour ce problème c'est bon.
    Par contre j'ai un autre problème maintenant, je n'arrive pas à persister.
    J'ai mis des try...catch... avec log4j et il me renvoie le log de débug que j'ai configuré ("Persist OK") et quand je regarde dans ma base pas de trace du persist en question.
    Alors je me demande si je ne doit pas mettre quelque chose pour faire de l'autoCommit ou à moins qu'il manque encore autre chose dans le fichier de conf de Spring ou Hibernate?

    NB: BD=MySQL (phpMyAdmin) et mon application est découpée en 2 projets, TazaWebapp pour les IHM et TazaSource pour le reste, c'est dans le projet TazaWebapp qu'il y a toute ma conf.

  17. #17
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
    Tu n'as pas les dépendances pour utiliser aspectj dans ton pom, ni la configuration. Regarde la documentation de spring pour l'utilisation d'@Tanstacitional.

    A+.

Discussions similaires

  1. [Batch] Problème avec Spring Batch
    Par meriem15 dans le forum Spring
    Réponses: 6
    Dernier message: 16/12/2009, 17h41
  2. Réponses: 0
    Dernier message: 06/08/2009, 17h10
  3. [Framework] Problème avec Spring AOP
    Par yashiro dans le forum Spring
    Réponses: 1
    Dernier message: 09/06/2009, 18h12
  4. Problème avec Spring Plugin
    Par H-bil dans le forum Struts 1
    Réponses: 3
    Dernier message: 16/12/2008, 15h21
  5. Problème avec Spring : java.io.FileNotFoundException
    Par lionel84 dans le forum Spring
    Réponses: 6
    Dernier message: 29/08/2008, 10h59

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