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 :

Problème association many to many


Sujet :

Hibernate Java

  1. #1
    Nouveau candidat au Club
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2009
    Messages : 2
    Par défaut Problème association many to many
    Bonjour,

    j'ai un problème avec Hibernate j'ai deux tables, TableA(id_b,textea) et TableB(id_b,texteb) et une table associative association(id_a,id_b)

    le problème c'est je n'arrive pas a insérer dans la table association, voici le code :

    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
    
    TableA.hbm.xml :
    
    <hibernate-mapping>
        <class name="TableA" table="TABLE_A" schema="TEST">
            <id name="idA" type="big_decimal">
                <column name="ID_A" precision="22" scale="0" />
                <generator class="assigned" />
            </id>
            <property name="texteA" type="string">
                <column name="TEXTE_A" length="100" />
            </property>
            <set name="tableBs" inverse="true" table="ASSOCIATION">
                <key>
                    <column name="ID_A" precision="22" scale="0" not-null="true" />
                </key>
                <many-to-many entity-name="TableB">
                    <column name="ID_B" precision="22" scale="0" not-null="true" />
                </many-to-many>
            </set>
        </class>
    </hibernate-mapping>
    
    TableB.hbm.xml :
    
    <hibernate-mapping>
        <class name="TableB" table="TABLE_B" schema="TEST">
            <id name="idB" type="big_decimal">
                <column name="ID_B" precision="22" scale="0" />
                <generator class="assigned" />
            </id>
            <property name="texteB" type="string">
                <column name="TEXTE_B" length="50" />
            </property>
            <set name="tableAs" inverse="false" table="ASSOCIATION">
                <key>
                    <column name="ID_B" precision="22" scale="0" not-null="true" />
                </key>
                <many-to-many entity-name="TableA">
                    <column name="ID_A" precision="22" scale="0" not-null="true" />
                </many-to-many>
            </set>
        </class>
    </hibernate-mapping>
    
    TableA.java :
    public class TableA implements java.io.Serializable {
    
    	private BigDecimal idA;
    	private String texteA;
    	private Set tableBs = new HashSet(0);
    
    	public TableA() {
    	}
    
    	public TableA(BigDecimal idA) {
    		this.idA = idA;
    	}
    
    	public TableA(BigDecimal idA, String texteA, Set tableBs) {
    		this.idA = idA;
    		this.texteA = texteA;
    		this.tableBs = tableBs;
    	}
    
    	public BigDecimal getIdA() {
    		return this.idA;
    	}
    
    	public void setIdA(BigDecimal idA) {
    		this.idA = idA;
    	}
    
    	public String getTexteA() {
    		return this.texteA;
    	}
    
    	public void setTexteA(String texteA) {
    		this.texteA = texteA;
    	}
    
    	public Set getTableBs() {
    		return this.tableBs;
    	}
    
    	public void setTableBs(Set tableBs) {
    		this.tableBs = tableBs;
    	}
    
    }
    
    TableB.java :
    
    public class TableB implements java.io.Serializable {
    
    	private BigDecimal idB;
    	private String texteB;
    	private Set tableAs = new HashSet(0);
    
    	public TableB() {
    	}
    
    	public TableB(BigDecimal idB) {
    		this.idB = idB;
    	}
    
    	public TableB(BigDecimal idB, String texteB, Set tableAs) {
    		this.idB = idB;
    		this.texteB = texteB;
    		this.tableAs = tableAs;
    	}
    
    	public BigDecimal getIdB() {
    		return this.idB;
    	}
    
    	public void setIdB(BigDecimal idB) {
    		this.idB = idB;
    	}
    
    	public String getTexteB() {
    		return this.texteB;
    	}
    
    	public void setTexteB(String texteB) {
    		this.texteB = texteB;
    	}
    
    	public Set getTableAs() {
    		return this.tableAs;
    	}
    
    	public void setTableAs(Set tableAs) {
    		this.tableAs = tableAs;
    	}
    
    }
    
    et la class Le code pour l'insertion un nouvel enregistrement dans table A:
    
    TableA tableA=new TableA();
    		TableB tableB=new TableB();
    		TableB tableB1=new TableB();
    		TableB tableB2=new TableB();
    		
    		Set<TableB> tableBs=new HashSet<TableB>();
    		
    		tableB.setIdB(BigDecimal.valueOf(1));
    		tableB.setTexteB("B1");
    		tableBs.add(tableB);
    		
    		tableB1.setIdB(BigDecimal.valueOf(2));
    		tableB1.setTexteB("Bb2");
    		tableBs.add(tableB1);
    		
    		tableB2.setIdB(BigDecimal.valueOf(3));
    		tableB2.setTexteB("Bb3");
    		tableBs.add(tableB2);
    		
    		tableA.setIdA(BigDecimal.valueOf(4));
    		tableA.setTexteA("A4");
    		tableA.setTableBs(tableBs);
    		
    		Session session= null;
    		Transaction tx=null;
    		try{
    			 session= HibernateUtil.openSession();
    			 tx=session.beginTransaction();
    			 session.save(tableA);
    			 tx.commit();
    			 session.flush();
    		}catch(Exception e){
    			if(tx!=null)
    				tx.rollback();
    		}finally{
    			session.close();
    		}
    il m'affiche sur la console :

    Hibernate: insert into TEST.TABLE_A (TEXTE_A, ID_A) values (?, ?)

    l'insertion dans la table association et tableB ne se fais pas.

    veuillez s'il vous plait m'aider a résoudre ce problème

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

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    Tu ne fais pas de save sur tes objets TableB.
    Il faut le faire avant de faire un save sur ton TableA, ou alors tu utilises l'attribut cascade.

    Et pense à utiliser les balises code quand tu postes un messages.

  3. #3
    Nouveau candidat au Club
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2009
    Messages : 2
    Par défaut j'ai encore le meme problème
    Merci pour votre réponse et dsl de ne pas avoir utiliser la balize code dans mon message initial.

    J'ai effectuer un session.save(..) sur les objets de TableB ca s'insère parfaitement, mais la table association non, elle est toujours vide. :s

  4. #4
    Membre confirmé
    Profil pro
    Sr. Software Engineer
    Inscrit en
    Août 2007
    Messages
    169
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Software Engineer

    Informations forums :
    Inscription : Août 2007
    Messages : 169
    Par défaut
    à ta place j'utiliserais la balise cascade. c'est plus simple.

Discussions similaires

  1. Réponses: 1
    Dernier message: 11/01/2013, 11h16
  2. [Mapping] Problème de reverse hibernate many-to-many
    Par cypriendt dans le forum Hibernate
    Réponses: 10
    Dernier message: 13/06/2012, 11h59
  3. Classe d'association, scaffolding et Many to many
    Par flotho dans le forum Grails
    Réponses: 0
    Dernier message: 09/02/2012, 18h56
  4. Problème avec une relation many-to-many
    Par jillthe1 dans le forum Doctrine2
    Réponses: 9
    Dernier message: 19/12/2011, 21h05
  5. Un peu de mal a comprendre le concepte "one-to-many" et "many-to-many"
    Par chriscoolletoubibe dans le forum Hibernate
    Réponses: 4
    Dernier message: 29/03/2007, 18h50

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