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 d'integration de hibernate dans spring


Sujet :

Spring Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mars 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 93
    Par défaut problème d'integration de hibernate dans spring
    Bonsoir les amies. J'essaye d'intégrer hibernate à un projet spring existant mais j'y arrive pas.J'ai lu qu'il faut remplacer le fichier de configuration de hibernate par un bean dans le fichier contexte de l'application et c'est ce que j'ai fait.
    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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
     
    <?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"
             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.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     
        <!-- the parent application context definition for the springapp application -->
     
        <bean id="productManager" class="springapp.service.SimpleProductManager">
            <property name="productDao" ref="productDao"/>
        </bean>
     
        <bean id="productDao" class="springapp.repository.JdbcProductDao">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    <!--
        <bean id="productDao" class="springapp.repository.p2">
        </bean>
    -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
     
        <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
     
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
     
        <bean id="exampleSessionFactory"
           class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
         <property name="dataSource">
             <ref local="dataSource"/>
         </property>
         <property name="mappingLocations">
             <list>
                 <value>
                     classpath:Product.hbm.xml
                 </value>
             </list>
         </property>
     
        </bean>
     
     
     
     
     
     
     
    </beans>
    je recois toujours une erreur 503. et quand j'enleve le bean exampleSessionFactory sa marche bien.
    Merci d'avance pour vos réponses.

  2. #2
    Membre averti Avatar de Hyperion99
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    62
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2006
    Messages : 62
    Par défaut
    Salut Manu


    Je te préviens tout de suite je suis aussi novice en la matière, je ne suis donc pas sur de te donner "LA" bonne réponse , je te donne cependant ma config qui fonctionne chez moi ...

    J'utilise mysql , spring et hibernate sur un server tomcat


    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
     
    <!--=========================== TRANSACTION ==================================-->
     
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>
     
     
        <bean id="transactionProxy" abstract="true"
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
     
        <!--=========================== DAO ==================================-->
     
     <!-- Pour ne pas utiliser d interceptor,  utilisez le bean faxDaoBean (classe FaxDaoImpl)-->
        <!--Pour utiliser des interceptor , utilisez le bean  faxDao( ci dessous)     -->
     
        <bean id="faxDaoBean" class="fax.dao.FaxDaoImpl" scope="prototype">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
     
        <bean id="faxDao" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces" value="fax.dao.IFaxDao"/>
            <property name="interceptorNames">
                <list>
                    <value>faxDaoInterceptor</value>
                    <value>faxDaoBean</value>
                </list>
            </property>
        </bean>
     
     
    <!--=========================== SESSSION FACTORY ==================================-->
     
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
              scope="singleton">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.c3p0.autoCommitOnClose">true</prop>
                    <prop key="hibernate.autoCommitOnClose">true</prop>
                    <prop key="hibernate.autocommit">true</prop>
                    <prop key="hibernate.connection.release_mode">auto</prop>
                    <prop key="hibernate.transaction.auto_close_session">true</prop>
                </props>
            </property>
        </bean>
    mon fichier hibernate.cfg.xml contient ma config hibernate et qui se trouve dans mon répertoire WEB-INF de mon server

    Encore une fois je précise qu'étant novice, ma config n'est peut être pas (pour ne pas dire "n'est pas" ...) la meilleur, elle contient peut être des erreurs.... Mais bon elle fonctionne ...


    En esperant que cela t'aidera ...

    a+

  3. #3
    Membre confirmé
    Inscrit en
    Mars 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 93
    Par défaut
    Salut Hyperion99.
    Je me suis pas mal documenté sur le sujet et j'ai trouvé pas mal de source qui concorde avec la tienne mais ma ce que je ne comprend pas c'est le bean transactionProxy que je trouve partout et que j'arrive pas a utilisé parce que je ne comprend pas son intérêt.

  4. #4
    Membre averti Avatar de Hyperion99
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    62
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2006
    Messages : 62
    Par défaut
    j'avoue que moi non plus je ne comprend pas réellement son interet.
    Je suppose qu'il est la pour pouvoir (en tout ca dans mon exemple) préciser quel type de propagation tu veux .
    Dans mon exemple c'est tout le temps mais j'ai vu des cas où tu peux préciser que tu veux que la propagation ne se fasse que pour les read, ou bien que pour les updates. Je crois que tu peux même préciser la granularité au niveau des methodes

    dans le code source de TransactionProxyFactoryBean on trouve l'exemple suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  abstract="true">;
        <property name="transactionManager" ref="transactionManager"/>;
        <property name="transactionAttributes">;
         <props>
           <prop key="insert*">PROPAGATION_REQUIRED</prop>;
           <prop key="update*">PROPAGATION_REQUIRED</prop>;
           <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
        </property>;
     </bea|
    et aussi

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    * Set properties with method names as keys and transaction attribute
    * descriptors (parsed via TransactionAttributeEditor) as values:
    * e.g. key = "myMethod", value = "PROPAGATION_REQUIRED,readOnly".
    Si tu trouves plus d'infos je suis preneur
    a+

  5. #5
    Membre confirmé
    Inscrit en
    Mars 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 93
    Par défaut
    peut tu me montre ta classe dao.

  6. #6
    Membre averti Avatar de Hyperion99
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    62
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2006
    Messages : 62
    Par défaut
    Salut Manu

    voici ma classe dao , à défaut de me répeter je te rappel que je suis débutant donc il se peut que cela ne soit pas le bonne exemple ...

    fichier de config 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
     
     <bean id="faxDaoInterceptor" class="fax.services.interceptor.FaxDaoInterceptor"/>
     
        <!--=========================== DAO ==================================-->
         <!--   Pour ne pas utiliser d interceptor,  utilisez le bean faxDaoBean (classe FaxDaoImpl)   -->
        <!--   Pour utiliser des interceptor , utilisez le bean faxDao   -->
     
        <bean id="faxDaoBean" class="fax.dao.FaxDaoImpl" scope="prototype">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
     
        <bean id="faxDao" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces" value="fax.dao.IFaxDao"/>
            <property name="interceptorNames">
                <list>
                    <value>faxDaoInterceptor</value>
                    <value>faxDaoBean</value>
                </list>
            </property>
        </bean>
    je te remet au passage ma config spring pour les transaction et la session factory
    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
     
      <!--=========================== TRANSACTION ==================================-->
     
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>
     
     
        <bean id="transactionProxy" abstract="true"
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
              scope="singleton">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.c3p0.autoCommitOnClose">true</prop>
                    <prop key="hibernate.autoCommitOnClose">true</prop>
                    <prop key="hibernate.autocommit">true</prop>
                    <prop key="hibernate.connection.release_mode">auto</prop>
                    <prop key="hibernate.transaction.auto_close_session">true</prop>
                </props>
            </property>
        </bean>

    classe 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
     
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     
    @Repository("faxDaoBean")
    @Scope("prototype")
    public class FaxDaoImpl extends AbstractFaxDao {
     
        @Autowired
        public FaxDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
            super.setSessionFactory(sessionFactory);
        }
     
        public IFaxModel saveFaxModel(IFaxModel faxModel) {
            save(faxModel);
            return faxModel;
        }
    et la classe AbstractFaxDao

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    public abstract class AbstractFaxDao extends HibernateDaoSupport implements IFaxDao {
     
     
        public Serializable save(IPersistantObject persistantObject) {
            HibernateTemplate ht = getHibernateTemplate();
            ht.saveOrUpdate(persistantObject);
            return persistantObject.getId();
        }
    }
    et au cas ou la classe service qui appel le 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
     
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.transaction.annotation.Propagation;
     
    @Service("faxServiceTarget")
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public class FaxService implements IFaxService {
     
        private IFaxDao faxDao;
     
     
        /**
         * @param faxDao : bean 'faxDaoBean' pour NE PAS utiliser d'interceptor
         *               bean 'faxDao' pour utiliser des interceptors
         */
        @Autowired
        public void setFaxDao(@Qualifier("faxDao") IFaxDao faxDao) {
            this.faxDao = faxDao;
        }
     
        public IFaxModel saveFaxModel(IFaxModel faxModel) {
            return getFaxDao().saveFaxModel(faxModel);
        }
     
     
        public IFaxDao getFaxDao() {
            return faxDao;
        }
    avec tout ca , normalement tu devrais t'en sortir

    a+

Discussions similaires

  1. Problème d'intégration de ehcache dans Spring
    Par anthony22360 dans le forum Persistance des données
    Réponses: 24
    Dernier message: 26/06/2013, 16h56
  2. [ehcache] Problème d'intégration de ehcache dans Spring
    Par anthony22360 dans le forum Spring
    Réponses: 0
    Dernier message: 25/06/2013, 19h55
  3. [Data] Problème de lazy avec hibernate et Spring
    Par Invité dans le forum Spring
    Réponses: 3
    Dernier message: 20/02/2008, 20h03
  4. Problème d'insertion hibernate dans la base
    Par sheura dans le forum Hibernate
    Réponses: 5
    Dernier message: 20/09/2007, 14h02
  5. [hibernate] comment integrer hibernate dans JBoss
    Par apoca dans le forum Wildfly/JBoss
    Réponses: 11
    Dernier message: 25/05/2007, 12h39

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