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 :

Lien Many-To-Many


Sujet :

Hibernate Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2012
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2012
    Messages : 116
    Par défaut Lien Many-To-Many
    Bonjour,

    Je rencontre actuellement un problème avec Hibernate, il s'agit pour moi d'atteindre une classe sans clé primaire, je m'explique,

    J'ai une classe "Technicien" :
    - id_technicien (clé primaire)
    - nom
    - prenom
    - ....

    J'ai une classe "Technique" :
    - id_technique (clé primaire)
    - nom_technique
    -...

    Et, j'ai une classe "Competence" :
    - id_technicien (clé étrangère)
    - id_technique (clé étrangère)
    - note

    Lors de la configuration d'Hibernate, ma classe "Competence" n'est pas apparue (j'utilise Netbeans 7.1 comme IDE).

    J'ai parcouru quelques tutoriels parlant du Many-To-Many, mais je ne suis pas sûr de partir dans la bonne direction, et également pas certain que ce soit la solution la plus simple ?

    Que pouvez-vous me dire à ce sujet ?

    D'avance merci pour votre aide.

  2. #2
    Membre Expert
    Avatar de fxrobin
    Homme Profil pro
    Architecte SI, Java Fan, API Manager
    Inscrit en
    Novembre 2007
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte SI, Java Fan, API Manager
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2007
    Messages : 875
    Par défaut
    et pourtant c'est bien ManyToMany la BONNE solution.
    Ta classe compétence disparait au profit de "List" dans chacune de tes entités.
    Ensuite en base de données, il y aura biensûr une table de jointure.

    Petit exemple :
    http://www.mkyong.com/hibernate/hibe...le-annotation/

    ici tu as la doc officielle pour faire une many-to-many Bidirectionnelle :
    http://docs.jboss.org/hibernate/core...ional-join-m2m

  3. #3
    Membre confirmé
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2012
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2012
    Messages : 116
    Par défaut
    Merci fxrobin pour ta rapidité.
    Dans mon message j'ai fait une erreur, dans ma table "Technicien" la clé primaire se nomme "id_ft", et du coup dans le table "Competence", la clé étrangère associée se nomme "id_technicien", dois-je m'attendre à de futurs problèmes ?

    Merci

  4. #4
    Membre Expert
    Avatar de fxrobin
    Homme Profil pro
    Architecte SI, Java Fan, API Manager
    Inscrit en
    Novembre 2007
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte SI, Java Fan, API Manager
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2007
    Messages : 875
    Par défaut
    non, car tu as un contrôle total sur le nom des champs, même des clés étrangères de tables de jointures.

  5. #5
    Membre confirmé
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2012
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2012
    Messages : 116
    Par défaut
    Merci de ta réponse,

    Je viens de terminer le tuto que tu m'as indiqué, mais des erreurs sortent :

    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
     
    Test du Many to Many
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.annotations.Version <clinit>
    Infos: Hibernate Annotations 3.3.1.GA
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Environment <clinit>
    Infos: Hibernate 3.2.5
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Environment <clinit>
    Infos: hibernate.properties not found
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Environment buildBytecodeProvider
    Infos: Bytecode provider name : cglib
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Environment <clinit>
    Infos: using JDK 1.4 java.sql.Timestamp handling
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration configure
    Infos: configuring from resource: /hibernate.cfg.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    Infos: Configuration resource: /hibernate.cfg.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Famille.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Responsable.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Localisation.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Technique.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Technicien.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Domaine.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Droit.hbm.xml
    nov. 08, 2012 1:28:40 PM org.hibernate.cfg.Configuration addResource
    Infos: Reading mappings from resource : domaine/Produit.hbm.xml
    Initial SessionFactory creation failed.org.hibernate.MappingException: Unable to load class declared as <mapping class="domaine/Technicien"/> in the configuration:
    Exception in thread "main" java.lang.ExceptionInInitializerError
    	at domaine.HibernateUtil.<clinit>(HibernateUtil.java:28)
    	at Main.main(Main.java:23)
    Caused by: org.hibernate.MappingException: Unable to load class declared as <mapping class="domaine/Technicien"/> in the configuration:
    	at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:638)
    	at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
    	at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1002)
    	at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:69)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
    	at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:990)
    	at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:69)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
    	at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
    	at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    	at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
    	at domaine.HibernateUtil.<clinit>(HibernateUtil.java:24)
    	... 1 more
    Caused by: java.lang.ClassNotFoundException: domaine/Technicien
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:186)
    	at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    	at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:635)
    	... 14 more
    Java Result: 1
    J'ai réaliser une classe "Main.java" en guise de classe test :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    public class Main {
     
        public static void main(String[] args) {
            System.out.println("Test du Many to Many");
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        }
    }
    Je sais la classe n'est pas complète, mais je procède par étape, hors ici ça bloque ?

    Une idée ?

    Visiblement il ne trouve pas ma classe, pourtant, elles sont bien déclarées :

    Fichier hibernat.cfg.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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/comptechv2</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
        <mapping resource="domaine/Famille.hbm.xml"/>
        <mapping resource="domaine/Responsable.hbm.xml"/>
        <mapping resource="domaine/Localisation.hbm.xml"/>
        <mapping resource="domaine/Technique.hbm.xml"/>
        <mapping resource="domaine/Technicien.hbm.xml"/>
        <mapping resource="domaine/Domaine.hbm.xml"/>
        <mapping resource="domaine/Droit.hbm.xml"/>
        <mapping resource="domaine/Produit.hbm.xml"/>
        <mapping class="domaine/Technicien"/>
        <mapping class="domaine/Technique"/>
      </session-factory>
    </hibernate-configuration>
    Merci

  6. #6
    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
    Attention, si dans ta classe Competence, tu veux d'autres attributs que les clés étrangères, un many-to-many ne sera pas suffisant.

  7. #7
    Membre Expert
    Avatar de fxrobin
    Homme Profil pro
    Architecte SI, Java Fan, API Manager
    Inscrit en
    Novembre 2007
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte SI, Java Fan, API Manager
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2007
    Messages : 875
    Par défaut
    la raison est là dans ta stacktrace :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Caused by: java.lang.ClassNotFoundException: domaine/Technicien
    Ta classe est bien dans le package "domaine" ?
    Elle est bien "publique" ?
    Constructeur vide public (ça c'est pour la suite).

    Montre ta classe Technicien

  8. #8
    Membre Expert
    Avatar de fxrobin
    Homme Profil pro
    Architecte SI, Java Fan, API Manager
    Inscrit en
    Novembre 2007
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte SI, Java Fan, API Manager
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2007
    Messages : 875
    Par défaut
    Citation Envoyé par fr1man Voir le message
    Attention, si dans ta classe Competence, tu veux d'autres attributs que les clés étrangères, un many-to-many ne sera pas suffisant.
    bienvu, je n'avais pas vu qu'il y avait "note" en attribut ...

    dans ces cas là c'est du

    many-to-one entre "Technicien" et "NoteCompetence"
    many-to-one entre "Competence" et "NoteCompetence"

    one-to-many entre "NoteCompetence" et "Technicien"
    one-to-many entre "NoteCompetence" et "Competence"

    Il te faut donc une classe "NoteCompetence"

    Pense "graphe d'objets" avant de penser "persistance".

    A+

  9. #9
    Membre confirmé
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2012
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2012
    Messages : 116
    Par défaut
    Je ne comprends pas pourquoi il faut créer une classe seulement pour un champs de ma table ?

    Pour essayer de vous éclairez voici mes classe :

    Technicien.java :
    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
     
    package domaine;
    // Generated 5 nov. 2012 11:48:38 by Hibernate Tools 3.2.1.GA
     
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.Table;
    import javax.persistence.UniqueConstraint;
     
     
     
    @Entity
    @Table(name="technicien", catalog="comptechv2", uniqueConstraints= {
    @UniqueConstraint(columnNames="nom"),
    @UniqueConstraint(columnNames="prenom"),
    @UniqueConstraint(columnNames="num_gsm"),
    @UniqueConstraint(columnNames="id_reponsable"),
    @UniqueConstraint(columnNames="id_loc"),
    @UniqueConstraint(columnNames="mobilite"),
    @UniqueConstraint(columnNames="date_naissance"),
    @UniqueConstraint(columnNames="id_domaine"),
    @UniqueConstraint(columnNames="depart"),
    })
    /**
     * Technicien generated by hbm2java
     */
    public class Technicien  implements java.io.Serializable {
     
         private String idFt;
         private Domaine domaine;
         private Localisation localisation;
         private Responsable responsable;
         private String nom;
         private String prenom;
         private String numGsm;
         private String mobilite;
         private String dateNaissance;
         private String depart;
     
         private Set<Technique> techniques = new HashSet<Technique>(0);
     
        public Technicien() {
        }
     
        public Technicien(String idFt, Domaine domaine, Localisation localisation, Responsable responsable, String nom, String prenom, String numGsm, String mobilite, String dateNaissance, String depart) {
           this.idFt = idFt;
           this.domaine = domaine;
           this.localisation = localisation;
           this.responsable = responsable;
           this.nom = nom;
           this.prenom = prenom;
           this.numGsm = numGsm;
           this.mobilite = mobilite;
           this.dateNaissance = dateNaissance;
           this.depart = depart;
        }
     
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        @Column(name="id_ft", unique= true, nullable= false)
            public String getIdFt() {
            return this.idFt;
        }
     
        public void setIdFt(String idFt) {
            this.idFt = idFt;
        }
     
        @Column(name="id_domaine",unique= true, nullable=false, length= 11)
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
         @Column(name="id_loc",unique= true, nullable=false, length= 11)
        public Localisation getLocalisation() {
            return this.localisation;
        }
     
        public void setLocalisation(Localisation localisation) {
            this.localisation = localisation;
        }
         @Column(name="id_reponsable",unique= true, nullable=false, length= 11)
        public Responsable getResponsable() {
            return this.responsable;
        }
     
        public void setResponsable(Responsable responsable) {
            this.responsable = responsable;
        }
         @Column(name="nom",unique=true, nullable=false)
        public String getNom() {
            return this.nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
         @Column(name="prenom",unique= true, nullable=false)
        public String getPrenom() {
            return this.prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
         @Column(name="num_gsm",unique= true, nullable=false, length= 50)
        public String getNumGsm() {
            return this.numGsm;
        }
     
        public void setNumGsm(String numGsm) {
            this.numGsm = numGsm;
        }
         @Column(name="mobilite",unique= true, nullable=false)
        public String getMobilite() {
            return this.mobilite;
        }
     
        public void setMobilite(String mobilite) {
            this.mobilite = mobilite;
        }
         @Column(name="date_naissance",unique= true, nullable=false, length= 50)
        public String getDateNaissance() {
            return this.dateNaissance;
        }
     
        public void setDateNaissance(String dateNaissance) {
            this.dateNaissance = dateNaissance;
        }
         @Column(name="depart",unique= true, nullable=false, length= 50)
        public String getDepart() {
            return this.depart;
        }
     
        public void setDepart(String depart) {
            this.depart = depart;
        }
     
    @ManyToMany(fetch= FetchType.LAZY, cascade= CascadeType.ALL)
    @JoinTable(name="competencev2", catalog= "comptechv2", joinColumns= {
    @JoinColumn(name= "id_technicien", nullable= false, updatable= false)}, inverseJoinColumns= {@JoinColumn(name= "id_technique", nullable= false, updatable= false)})
     
        /**
         * @return the techniques
         */
        public Set<Technique> getTechniques() {
            return techniques;
        }
     
        /**
         * @param techniques the techniques to set
         */
        public void setTechniques(Set<Technique> techniques) {
            this.techniques = techniques;
        }
     
    }
    Technique.java :
    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
     
    package domaine;
    // Generated 5 nov. 2012 11:48:38 by Hibernate Tools 3.2.1.GA
     
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.*;
     
     
    @Entity
    @Table(name= "technique", catalog= "comptechv2")
    /**
     * Technique generated by hbm2java
     */
    public class Technique  implements java.io.Serializable {
     
     
         private Integer idTechnique;
         private Produit produit;
         private Domaine domaine;
         private String categorieTechnique;
         private String nomTechnique;
     
         private Set<Technicien> techniciens = new HashSet<Technicien>(0);
     
        public Technique() {
        }
     
        public Technique(Produit produit, Domaine domaine, String categorieTechnique, String nomTechnique) {
           this.produit = produit;
           this.domaine = domaine;
           this.categorieTechnique = categorieTechnique;
           this.nomTechnique = nomTechnique;
        }
     
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        @Column(name="id_technique", unique= true, nullable= false)
     
        public Integer getIdTechnique() {
            return this.idTechnique;
        }
     
        public void setIdTechnique(Integer idTechnique) {
            this.idTechnique = idTechnique;
        }
     
        @Column(name="code_produit", nullable=false, length= 11)
        public Produit getProduit() {
            return this.produit;
        }
     
        public void setProduit(Produit produit) {
            this.produit = produit;
        }
         @Column(name="id_domaine",unique=true, nullable=false, length= 11)
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
         @Column(name="categorie_technique", nullable=false)
        public String getCategorieTechnique() {
            return this.categorieTechnique;
        }
     
        public void setCategorieTechnique(String categorieTechnique) {
            this.categorieTechnique = categorieTechnique;
        }
         @Column(name="nom_technique",unique= true, nullable=false)
        public String getNomTechnique() {
            return this.nomTechnique;
        }
     
        public void setNomTechnique(String nomTechnique) {
            this.nomTechnique = nomTechnique;
        }
     
        @ManyToMany(fetch= FetchType.LAZY, mappedBy= "techniques")
        /**
         * @return the techniciens
         */
        public Set<Technicien> getTechniciens() {
            return techniciens;
        }
     
        /**
         * @param techniciens the techniciens to set
         */
        public void setTechniciens(Set<Technicien> techniciens) {
            this.techniciens = techniciens;
        }
     
    }
    Competencev2.java :
    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package domaine;
     
    import java.io.Serializable;
    import java.util.List;
     
    public class Competencev2 implements Serializable{
     
        private String id_technicien;
        private Integer id_technique;
        private String note;
     
        private List<String> selectedCompetence;
     
        public Competencev2(){
     
        }
     
        public Competencev2(String id_technicien, Integer id_technique, String note){
            this.id_technicien = id_technicien;
            this.id_technique = id_technique;
            this.note = note;
        }
     
        /**
         * @return the id_technicien
         */
        public String getId_technicien() {
            return id_technicien;
        }
     
        /**
         * @param id_technicien the id_technicien to set
         */
        public void setId_technicien(String id_technicien) {
            this.id_technicien = id_technicien;
        }
     
        /**
         * @return the id_technique
         */
        public Integer getId_technique() {
            return id_technique;
        }
     
        /**
         * @param id_technique the id_technique to set
         */
        public void setId_technique(Integer id_technique) {
            this.id_technique = id_technique;
        }
     
        /**
         * @return the note
         */
        public String getNote() {
            return note;
        }
     
        /**
         * @param note the note to set
         */
        public void setNote(String note) {
            this.note = note;
        }
     
        /**
         * @return the selectedCompetence
         */
        public List<String> getSelectedCompetence() {
            return selectedCompetence;
        }
     
        /**
         * @param selectedCompetence the selectedCompetence to set
         */
        public void setSelectedCompetence(List<String> selectedCompetence) {
            this.selectedCompetence = selectedCompetence;
        }
     
    }

  10. #10
    Membre Expert
    Avatar de fxrobin
    Homme Profil pro
    Architecte SI, Java Fan, API Manager
    Inscrit en
    Novembre 2007
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Architecte SI, Java Fan, API Manager
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2007
    Messages : 875
    Par défaut
    Parce qu'il faut que tu penses "modèle objet" à la place de modèle de données.
    Il faut bien que ta note existe qqs part.
    Elle n'est ni dans "Technicien", ni dans "Competence".
    Elle est dans une classe "NoteCompetence" par exemple :

    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
     
    class Technicien
    {
       List <NoteCompetence> notes; // pas toujours utile
    }
     
    class Competence
    {
        List <NoteCompetence> notes; // pas toujours utile
    }
     
    class NoteCompetence
    {
      Technicien technicienConcerne;
      Competence competenceConcerne;
      Decimal note;
    }

  11. #11
    Membre confirmé
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2012
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2012
    Messages : 116
    Par défaut
    Bonjour,

    D'après vos conseils, j'essaye de réaliser le mapping me permettant d'atteindre ma table "Competencev2" de ma base de données. Pour vous aider j'ai réaliser un schéma :
    http://www.casimages.com/img.php?i=1...2162614809.png

    J'ai également créé la classe NoteCompetence.java :
    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package domaine;
     
    /**
     *
     * @author NHDF1037
     */
    public class NoteCompetence {
     
        private Technicien technicienConcerne;
        private Competencev2 competenceConcerne;
        private String note;
     
        /**
         * Creates a new instance of NoteCompetence
         */
        public NoteCompetence() {
        }
     
        /**
         * @return the technicienConcerne
         */
        public Technicien getTechnicienConcerne() {
            return technicienConcerne;
        }
     
        /**
         * @param technicienConcerne the technicienConcerne to set
         */
        public void setTechnicienConcerne(Technicien technicienConcerne) {
            this.technicienConcerne = technicienConcerne;
        }
     
        /**
         * @return the competenceConcerne
         */
        public Competencev2 getCompetenceConcerne() {
            return competenceConcerne;
        }
     
        /**
         * @param competenceConcerne the competenceConcerne to set
         */
        public void setCompetenceConcerne(Competencev2 competenceConcerne) {
            this.competenceConcerne = competenceConcerne;
        }
     
        /**
         * @return the note
         */
        public String getNote() {
            return note;
        }
     
        /**
         * @param note the note to set
         */
        public void setNote(String note) {
            this.note = note;
        }
    }
    Ma classe Technicien.java :
    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
     
    package domaine;
    // Generated 5 nov. 2012 11:48:38 by Hibernate Tools 3.2.1.GA
     
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import javax.persistence.*;
     
    @Entity
    @Table(name="technicien", catalog="comptechv2", uniqueConstraints= {
    @UniqueConstraint(columnNames="nom"),
    @UniqueConstraint(columnNames="prenom"),
    @UniqueConstraint(columnNames="num_gsm"),
    @UniqueConstraint(columnNames="id_reponsable"),
    @UniqueConstraint(columnNames="id_loc"),
    @UniqueConstraint(columnNames="mobilite"),
    @UniqueConstraint(columnNames="date_naissance"),
    @UniqueConstraint(columnNames="id_domaine"),
    @UniqueConstraint(columnNames="depart"),
    })
    /**
     * Technicien generated by hbm2java
     */
    public class Technicien  implements java.io.Serializable {
     
         private String idFt;
         private Domaine domaine;
         private Localisation localisation;
         private Responsable responsable;
         private String nom;
         private String prenom;
         private String numGsm;
         private String mobilite;
         private String dateNaissance;
         private String depart;
     
         private Set<Technique> techniques = new HashSet<Technique>(0);
     
         private List<NoteCompetence> notes;
     
        public Technicien() {
        }
     
        public Technicien(String idFt, Domaine domaine, Localisation localisation, Responsable responsable, String nom, String prenom, String numGsm, String mobilite, String dateNaissance, String depart) {
           this.idFt = idFt;
           this.domaine = domaine;
           this.localisation = localisation;
           this.responsable = responsable;
           this.nom = nom;
           this.prenom = prenom;
           this.numGsm = numGsm;
           this.mobilite = mobilite;
           this.dateNaissance = dateNaissance;
           this.depart = depart;
        }
     
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        @Column(name="id_ft", unique= true, nullable= false)
            public String getIdFt() {
            return this.idFt;
        }
     
        public void setIdFt(String idFt) {
            this.idFt = idFt;
        }
     
        @Column(name="id_domaine",unique= true, nullable=false, length= 11)
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
         @Column(name="id_loc",unique= true, nullable=false, length= 11)
        public Localisation getLocalisation() {
            return this.localisation;
        }
     
        public void setLocalisation(Localisation localisation) {
            this.localisation = localisation;
        }
         @Column(name="id_reponsable",unique= true, nullable=false, length= 11)
        public Responsable getResponsable() {
            return this.responsable;
        }
     
        public void setResponsable(Responsable responsable) {
            this.responsable = responsable;
        }
         @Column(name="nom",unique=true, nullable=false)
        public String getNom() {
            return this.nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
         @Column(name="prenom",unique= true, nullable=false)
        public String getPrenom() {
            return this.prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
         @Column(name="num_gsm",unique= true, nullable=false, length= 50)
        public String getNumGsm() {
            return this.numGsm;
        }
     
        public void setNumGsm(String numGsm) {
            this.numGsm = numGsm;
        }
         @Column(name="mobilite",unique= true, nullable=false)
        public String getMobilite() {
            return this.mobilite;
        }
     
        public void setMobilite(String mobilite) {
            this.mobilite = mobilite;
        }
         @Column(name="date_naissance",unique= true, nullable=false, length= 50)
        public String getDateNaissance() {
            return this.dateNaissance;
        }
     
        public void setDateNaissance(String dateNaissance) {
            this.dateNaissance = dateNaissance;
        }
         @Column(name="depart",unique= true, nullable=false, length= 50)
        public String getDepart() {
            return this.depart;
        }
     
        public void setDepart(String depart) {
            this.depart = depart;
        }
     
    @ManyToMany(fetch= FetchType.LAZY, cascade= CascadeType.ALL)
    @JoinTable(name="competencev2", catalog= "comptechv2", joinColumns= {
    @JoinColumn(name= "id_technicien", nullable= false, updatable= false)}, inverseJoinColumns= {@JoinColumn(name= "id_technique", nullable= false, updatable= false)})
     
        /**
         * @return the techniques
         */
        public Set<Technique> getTechniques() {
            return techniques;
        }
     
        /**
         * @param techniques the techniques to set
         */
        public void setTechniques(Set<Technique> techniques) {
            this.techniques = techniques;
        }
     
        /**
         * @return the notes
         */
        public List<NoteCompetence> getNotes() {
            return notes;
        }
     
        /**
         * @param notes the notes to set
         */
        public void setNotes(List<NoteCompetence> notes) {
            this.notes = notes;
        }
     
    }

    Ma classe Technique.java :

    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
     
    package domaine;
    // Generated 5 nov. 2012 11:48:38 by Hibernate Tools 3.2.1.GA
     
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.*;
     
     
    @Entity
    @Table(name= "technique", catalog= "comptechv2")
    /**
     * Technique generated by hbm2java
     */
    public class Technique  implements java.io.Serializable {
     
     
         private Integer idTechnique;
         private Produit produit;
         private Domaine domaine;
         private String categorieTechnique;
         private String nomTechnique;
     
         private Set<Technicien> techniciens = new HashSet<Technicien>(0);
     
        public Technique() {
        }
     
        public Technique(Produit produit, Domaine domaine, String categorieTechnique, String nomTechnique) {
           this.produit = produit;
           this.domaine = domaine;
           this.categorieTechnique = categorieTechnique;
           this.nomTechnique = nomTechnique;
        }
     
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        @Column(name="id_technique", unique= true, nullable= false)
     
        public Integer getIdTechnique() {
            return this.idTechnique;
        }
     
        public void setIdTechnique(Integer idTechnique) {
            this.idTechnique = idTechnique;
        }
     
        @Column(name="code_produit", nullable=false, length= 11)
        public Produit getProduit() {
            return this.produit;
        }
     
        public void setProduit(Produit produit) {
            this.produit = produit;
        }
         @Column(name="id_domaine",unique=true, nullable=false, length= 11)
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
         @Column(name="categorie_technique", nullable=false)
        public String getCategorieTechnique() {
            return this.categorieTechnique;
        }
     
        public void setCategorieTechnique(String categorieTechnique) {
            this.categorieTechnique = categorieTechnique;
        }
         @Column(name="nom_technique",unique= true, nullable=false)
        public String getNomTechnique() {
            return this.nomTechnique;
        }
     
        public void setNomTechnique(String nomTechnique) {
            this.nomTechnique = nomTechnique;
        }
     
        @ManyToMany(fetch= FetchType.LAZY, mappedBy= "techniques")
        /**
         * @return the techniciens
         */
        public Set<Technicien> getTechniciens() {
            return techniciens;
        }
     
        /**
         * @param techniciens the techniciens to set
         */
        public void setTechniciens(Set<Technicien> techniciens) {
            this.techniciens = techniciens;
        }
     
    }
    Ces classes fonctionnent de la manière suivant :

    Un technicien dispose d'une fiche de compétence sur l'ensemble des techniques, avec une notre par technique (id_technique) allant de 0 à 4.

    Mes recherches dans mon application sont donc de ce type, exemple :
    "Recherchez l'ensemble des techniciens, ayant la note 3 sur l'id_Technique 110"

    Sommes nous d'accord sur le Mapping que je dois mettre en place :

    many-to-one entre Technicien et NoteCompetence
    many-to-one entre Competence et NoteCompetence

    one-to-many entre NoteCompetence et Technicien
    one-to-many entre NoteCompetence et Competence

    D'avance merci
    Fred

Discussions similaires

  1. [Core] Many-to-many uni-directionnelle - table lien jamais remplie
    Par l'Ermite Misanthrope dans le forum Hibernate
    Réponses: 7
    Dernier message: 24/04/2015, 08h26
  2. Réponses: 14
    Dernier message: 22/03/2011, 15h01
  3. 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
  4. [EJB2.1 Entity] [XDoclet][JBoss] CMR - Many to Many Relation
    Par dauggui dans le forum Java EE
    Réponses: 4
    Dernier message: 24/04/2006, 11h45
  5. [hibernate]relation many-to-many
    Par quilo dans le forum Hibernate
    Réponses: 5
    Dernier message: 20/12/2005, 10h07

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