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

Hibernate Java Discussion :

commit sous hibernate + spring


Sujet :

Hibernate Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Points : 44
    Points
    44
    Par défaut commit sous hibernate + spring
    Voila, dernière question... tout marche ou presque.

    j'ai un application context comme ca :
    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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">
     
    <beans>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
             <property name="url"><value>jdbc:hsqldb:/home/yannmauron/toxico_db</value></property>
            <property name="username"><value>sa</value></property>
            <property name="password"><value></value></property>
        </bean>
     
     <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
            <list>
                <value>com/genebio/toxico/compound/CompoundImpl.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.connection.autocommit">true</prop>
        </props>
        </property>
    </bean>
     
        <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
        <bean id="CompoundDAO" class="com.genebio.toxico.compound.CompoundDAOImpl">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
    </beans>
    un mapping comme ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     
    <hibernate-mapping package="com.genebio.toxico.compound" schema="PUBLIC">
    	<class table="COMPOUND" name="CompoundImpl">
    		<id name="id" type="int" column="COMPOUND_ID"></id>
    		<property name="cid" type="string" column="COMPOUND_CID" />
    		<property name="SMILE" type="string" column="COMPOUND_SMILE" />
    	</class>
     
    </hibernate-mapping>
    mon objet qui est :
    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
     
    package com.genebio.toxico.compound;
     
    /**
     * Basic class for the compound object
     * 
     * @author Yann
     *
     */
     
    import java.util.ArrayList;
    import java.io.Serializable;
     
    public class CompoundImpl extends CompoundImplBase implements Serializable{
     
    	public CompoundImpl() {
    	}
     
    	protected String cid;
    	protected int id;
    	protected String SMILE;
    	protected ArrayList<CompoundDescriptor> compoundDescriptors;
    	protected ArrayList<CompoundName> compoundNames;
    	protected ArrayList<CompoundClassification> compoundclassification;
    	protected ArrayList<CompoundCorrelation> compoundcorrelations;
    	protected ArrayList<CompoundXref> compoundXrefs;
     
    	//minimum information for a compound
    	public CompoundImpl(int id, String cid, String SMILE){
    		this.id 					= id;
    		this.cid 					= cid;
    		this.SMILE 					= SMILE;	
    	}
     
    	//primary information for a compound
    	public CompoundImpl(int id, String cid, String SMILE, 
    			ArrayList<CompoundDescriptor> compoundDescriptors, 
    			ArrayList<CompoundName> compoundNames, 
    			ArrayList<CompoundXref> compoundXrefs
    			){
     
    		this.id 					= id;
    		this.cid 					= cid;
    		this.SMILE 					= SMILE;
    		this.compoundDescriptors	= compoundDescriptors;
    		this.compoundNames			= compoundNames;
    		this.compoundXrefs			= compoundXrefs;
    }
     
    	//future information for a compound (correlation and classification informations are added)
    	public CompoundImpl(int id, String cid, String SMILE, 
    					ArrayList<CompoundDescriptor> compoundDescriptors, 
    					ArrayList<CompoundName> compoundNames, 
    					ArrayList<CompoundXref> compoundXrefs,
    					ArrayList<CompoundClassification> compoundclassification, 
    					ArrayList<CompoundCorrelation> compoundcorrelations 
    					){
     
    		this.id 					= id;
    		this.cid 					= cid;
    		this.SMILE 					= SMILE;
    		this.compoundclassification	= compoundclassification;
    		this.compoundcorrelations	= compoundcorrelations;
    		this.compoundDescriptors	= compoundDescriptors;
    		this.compoundNames			= compoundNames;
    		this.compoundXrefs			= compoundXrefs;
    	}
     
     
    	public String getSMILE(){
    		return SMILE;
    	}
     
    	public void setSMILE(String SMILE){
    		this.SMILE=SMILE;
    	}
     
    	public ArrayList<CompoundClassification> getCompoundclassification() {
    		return compoundclassification;
    	}
     
    	public void setCompoundclassification(
    			ArrayList<CompoundClassification> compoundclassification) {
    		this.compoundclassification = compoundclassification;
    	}
     
    	public ArrayList<CompoundCorrelation> getCompoundcorrelations() {
    		return compoundcorrelations;
    	}
     
    	public void setCompoundcorrelations(
    			ArrayList<CompoundCorrelation> compoundcorrelations) {
    		this.compoundcorrelations = compoundcorrelations;
    	}
     
    	public ArrayList<CompoundDescriptor> getCompoundDescriptors() {
    		return compoundDescriptors;
    	}
     
    	public void setCompoundDescriptors(
    			ArrayList<CompoundDescriptor> compoundDescriptors) {
    		this.compoundDescriptors = compoundDescriptors;
    	}
     
    	public ArrayList<CompoundName> getCompoundNames() {
    		return compoundNames;
    	}
     
    	public void setCompoundNames(ArrayList<CompoundName> compoundNames) {
    		this.compoundNames = compoundNames;
    	}
     
    	public ArrayList<CompoundXref> getCompoundXrefs() {
    		return compoundXrefs;
    	}
     
    	public void setCompoundXrefs(ArrayList<CompoundXref> compoundXrefs) {
    		this.compoundXrefs = compoundXrefs;
    	}
     
    	public int getId() {
    		return id;
    	}
     
    	public String getCid() {
    		return cid;
    	}
     
    	public void setId(int id) {
    		this.id=id;
    	}
     
    	public void setCid(String cid) {
    		this.cid=cid;
    	}
     
     
    }
    et enfin l'implémentation de 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
    22
    23
    24
    25
     
     
    package com.genebio.toxico.compound;
     
    import java.util.List;
    import com.genebio.toxico.compound.Compound;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     
     
    public class CompoundDAOImpl extends HibernateDaoSupport implements CompoundDAO{
     
    	public void saveCompound(CompoundImpl compound) {
    		getHibernateTemplate().saveOrUpdate(compound);
    	}
     
    	public CompoundImpl getCompound(int id) {
    		return (CompoundImpl) getHibernateTemplate().get(CompoundImpl.class, id);
    	}
     
    	public void removeCompound(int id) {
    		Object compound = getHibernateTemplate().load(Compound.class, id);
    		getHibernateTemplate().delete(compound);
    	}
     
    }
    je fais le junit test comme ca :
    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
     
    package com.genebio.toxico.compound;
     
    import static org.junit.Assert.assertEquals;
     
    import org.hibernate.classic.Session;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import junit.framework.TestCase;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    public class CompoundImplDAOTest {
     
    private CompoundImpl compoundImpl = null;
    private CompoundDAOImpl compoundDAO = null;
    private ApplicationContext ctx = null;
     
    	@Before
        public void setUp() throws Exception {
        	String[] paths = {"applicationContext.xml"};
            ctx = new ClassPathXmlApplicationContext(paths);
        	compoundDAO = (CompoundDAOImpl)  ctx.getBean("CompoundDAO");
    	}
     
     
    	@Test
    	    public void testSaveRecord() throws Exception {
    		        compoundImpl = new CompoundImpl();
    		        compoundImpl.setId(3);
    		        compoundImpl.setCid("1234567");
    		        compoundImpl.setSMILE("lkjfdg");
    		        compoundDAO.saveCompound(compoundImpl);
    		        Assert.assertNotNull("primary key assigned", compoundImpl.getId());
    		    }
     
    	@Test
        public void testloadRecord() throws Exception {
    		CompoundImpl compound = compoundDAO.getCompound(2);
    		System.out.println(compound.toString());
    	    }
     
    }
    Et du coup tout passe (j'ai mis une entrés a la main avec une id 2 dans la base et il la lit bien), mais le test testSaveRecord() n'enregistre rien dans la base :-( Une idée?

  2. #2
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    En fait, dans ton fichier spring, tu ne définis pas de transaction pour ton DAO,
    donc il n'y pas de commit, donc pas de sauvegarde en base.

    Et puis selon moi, la transaction se gère au niveau supérieur, mais pas au niveau DAO.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Points : 44
    Points
    44
    Par défaut
    Ha... et pour un débutant comme moi, on déclare comment une transaction ? C'est pas le simple fait de déclarer mon transactionmanager qui le fait ?

  4. #4
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Il faut déclarer ton transactionManager, comme tu l'as fait, et ensuite le relier à une classe, en l'occurence ton DAO même si je ne pense pas
    que ce soit bien de le faire sur le DAO.

    J'ai pas le code sous la main, désolé.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Points : 44
    Points
    44
    Par défaut
    D'ac, je comprend bien,
    mais est-ce que je ne le fais pas quand je fait reférence à ma sessionfactory, qui elle inclue mon fichier de mapping ?

  6. #6
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    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
    <bean id="dataSource" 
       class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName">
            <value>java:comp/env/jdbc/petclinic</value>
         </property>
    </bean>
     
    <bean id="transactionManager" 
       class="org.springframework.transaction.jta.JtaTransactionManager"/>
     
    <bean id="clinicTarget" 
       class="org.springframework.samples.petclinic.jdbc.JdbcClinic">
        <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>
     
    <bean id="clinic" 
       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="target"><ref bean="clinicTarget"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="store*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    Voilà un exemple de la démarche à suivre.
    Tu déclares un transaction manager, (comme tu l'as fait).
    Ensuite tu déclares les objets sur lesquels tu veux appliquer ta transaction:
    ici c'est le bean clinicTarget qui correspond à ton DAO.
    Enfin, tu fais la lisaison, entre ton bean et ton transaction manager, comme c'est fait dans le bean clinic.

    En espérant que ce soit plus clair pour toi.

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Points : 44
    Points
    44
    Par défaut
    Ca marche, mais pour ma culture personnelle, je me demande :

    Pourquoi est-ce que quand je déclare une sessionFactory qui contient mon dao (ou plutot mon fichier de mapping) et que je fais référence à cette sessionFactory dans mon trnsactionManager, ca ne reviens pas au meme?

  8. #8
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Parce qu'il faut bien délimiter tes transactions, définir sur quelles
    méthodes de quelles objets elles s'appliquent.
    Tu as simplement défini un transaction manager.

Discussions similaires

  1. Probleme de lecture Sous Hibernate
    Par Invité dans le forum Hibernate
    Réponses: 11
    Dernier message: 24/03/2010, 11h16
  2. [Hibernate - Spring] Spring => Version 2 d'Hibernate?
    Par cicolas dans le forum Hibernate
    Réponses: 2
    Dernier message: 30/05/2006, 16h22
  3. createblob sous hibernate
    Par zizou771 dans le forum PostgreSQL
    Réponses: 1
    Dernier message: 26/05/2006, 14h38
  4. [hibernate][spring]requete select from where IN
    Par whilecoyote dans le forum Hibernate
    Réponses: 1
    Dernier message: 07/04/2006, 09h06
  5. [Hibernate][Spring] Session Hibernate initialisée
    Par mauvais_karma dans le forum Hibernate
    Réponses: 12
    Dernier message: 08/08/2005, 13h07

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