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

JPA Java Discussion :

Mapping JPA en J2EE


Sujet :

JPA 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 Mapping JPA en J2EE
    Bonjour à tous,

    Je rencontre un problème sur mon application java web. En quelques mots, ma base de donnée contient une table dont la clé primaire est formée par deux clés étrangères, du coup, en utilisant HIBERNATE, celle-ci n'est pas atteignable par mes requêtes HQL.

    J'ai trouvé ce tuto : http://blogtechno.novediagroup.com/m...pplementaires/

    Il correspond parfaitement à mon problème, sauf que je ne comprend pas le rôle de ces deux fonctions, et surtout comment les adapter à mon cas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    @Override
    public boolean equals(Object obj) {
    ?
    }
     
    @Override
    public int hashCode() {
    ?
    }
    Merci d'avance pour votre aide,

    Cordialement
    Fred

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    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 482
    Par défaut
    equals permet de savoir si deux instances sont identiques. Quand on redéfinis equals, il faut aussi redéfinir hashcode. Ensemble, ces deux méthodes permettent aux Set d'utiliser ton objet correctement. Donc equals doit renvoyer true si on est identique à l'objet passé en paramètre. Hashcode doit retourner un identifiant stable de ton objet (tant que ton objet ne change pas, hashcode doit retourner la même valeur).

    Si tu n'a pas envie de te casser la tête, demande à ton ide de générer ce code à partir des champs qui t'intéressent.


    Pourquoi et comment redéfinir la méthode equals() ?
    Pourquoi et comment redéfinir la méthode hashCode() ?

  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 pour ces réponses
    Un autre problème apparait :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    employe.getConcepts().add(this);
    concept.getEmployes().add(this);
    A cette étape, dans mon cas ces deux lignes sont en erreur avec pour indications :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    no suitable method found for add(.....)
    method java.util.set.add (....) is not applicable
    Je n'arrive pas à régler ce soucis.

    D'avance merci

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    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 482
    Par défaut
    ce ne sont probablement pas des Set<EmployeConcept>

  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, en effet c'était cela.

    Le mapping ne fonctionne toujours pas, et je pense m'être trompé de type d'association.
    Ce n'est pas du Many-to-one que je dois faire mais du Many-to-Many car :

    J'ai une table technicien
    J'ai une table produit
    J'ai une table compétence (celle dont la clé primaire est formée par deux clés étrangères)

    Un technicien peut avoir des compétences sur plusieurs produits et un produit peut être affecté sur plusieurs techniciens.

    Tout au long de mon étude j'ai suivi ce tuto : http://blogtechno.novediagroup.com/m...pplementaires/

    Mes tables sont nommées :
    Technicien
    Technique
    Competencev2

    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
     
    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.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
     
     
     
    /**
     * Technicien generated by hbm2java
     */
    @Entity
    public class Technicien implements java.io.Serializable {
     
        @Id
        @Column(name="id_ft")
        private String idFt;
     
        @OneToMany(mappedBy="technicien")
        private Set<Competencev2> techniques = new HashSet<Competencev2>();
     
        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;
     
     
        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;
        }
     
        public String getIdFt() {
            return this.idFt;
        }
     
        public void setIdFt(String idFt) {
            this.idFt = idFt;
        }
     
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
     
        public Localisation getLocalisation() {
            return this.localisation;
        }
     
        public void setLocalisation(Localisation localisation) {
            this.localisation = localisation;
        }
     
        public Responsable getResponsable() {
            return this.responsable;
        }
     
        public void setResponsable(Responsable responsable) {
            this.responsable = responsable;
        }
     
        public String getNom() {
            return this.nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getPrenom() {
            return this.prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        public String getNumGsm() {
            return this.numGsm;
        }
     
        public void setNumGsm(String numGsm) {
            this.numGsm = numGsm;
        }
     
        public String getMobilite() {
            return this.mobilite;
        }
     
        public void setMobilite(String mobilite) {
            this.mobilite = mobilite;
        }
     
        public String getDateNaissance() {
            return this.dateNaissance;
        }
     
        public void setDateNaissance(String dateNaissance) {
            this.dateNaissance = dateNaissance;
        }
     
        public String getDepart() {
            return this.depart;
        }
     
        public void setDepart(String depart) {
            this.depart = depart;
        }
     
        /**
         * @return the techniques
         */
        public Set<Competencev2> getTechniques() {
            return techniques;
        }
     
        /**
         * @param techniques the techniques to set
         */
        public void setTechniques(Set<Competencev2> techniques) {
            this.techniques = techniques;
        }
     
    }
    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
     
    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.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
     
    /**
     * Technique generated by hbm2java
     */
    @Entity
    public class Technique  implements java.io.Serializable {
     
        @Id
        @Column(name="id_technique")
         private Integer idTechnique;
     
        @OneToMany(mappedBy="technique")
        private Set<Competencev2> techniciens = new HashSet<Competencev2>();
     
         private Produit produit;
         private Domaine domaine;
         private String categorieTechnique;
         private String nomTechnique;
     
     
        public Technique() {
        }
     
        public Technique(Produit produit, Domaine domaine, String categorieTechnique, String nomTechnique) {
           this.produit = produit;
           this.domaine = domaine;
           this.categorieTechnique = categorieTechnique;
           this.nomTechnique = nomTechnique;
        }
     
     
        public Integer getIdTechnique() {
            return this.idTechnique;
        }
     
        public void setIdTechnique(Integer idTechnique) {
            this.idTechnique = idTechnique;
        }
     
        public Produit getProduit() {
            return this.produit;
        }
     
        public void setProduit(Produit produit) {
            this.produit = produit;
        }
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
        public String getCategorieTechnique() {
            return this.categorieTechnique;
        }
     
        public void setCategorieTechnique(String categorieTechnique) {
            this.categorieTechnique = categorieTechnique;
        }
        public String getNomTechnique() {
            return this.nomTechnique;
        }
     
        public void setNomTechnique(String nomTechnique) {
            this.nomTechnique = nomTechnique;
        }
     
        /**
         * @return the techniciens
         */
        public Set<Competencev2> getTechniciens() {
            return techniciens;
        }
     
        /**
         * @param techniciens the techniciens to set
         */
        public void setTechniciens(Set<Competencev2> techniciens) {
            this.techniciens = techniciens;
        }
    }
    Classe 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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package domaine;
     
    import java.io.Serializable;
    import java.util.List;
    import javax.faces.model.SelectItem;
    import javax.persistence.*;
     
    /**
     *
     * @author NHDF1037
     */
    //Annotation pour déclarer la table de la bd
    @Entity
    public class Competencev2 {
        @EmbeddedId
        private CompetenceId id;
     
        @Column(name="note")
        private String note;
     
        @ManyToOne
        @JoinColumn(name="id_technicien", insertable=false, updatable=false)
        private Technicien technicien;
     
        @ManyToOne
        @JoinColumn(name="id_technique", insertable=false, updatable=false)
        private Technique technique;
     
        private List<SelectItem> selectedCompetence;
     
        public Competencev2(){
     
        }
     
        public Competencev2(Technicien technicien, Technique technique,String note){
            this.technicien = technicien;
            this.technique = technique;
     
            technicien.getTechniques().add(this);
            technique.getTechniciens().add(this);
     
            this.id = new CompetenceId(technicien.getIdFt(), technique.getIdTechnique());
            this.note = note;
        }
     
        /**
         * @return the technicien
         */
        public Technicien getTechnicien() {
            return technicien;
        }
     
        /**
         * @return the technique
         */
        public Technique getTechnique() {
            return technique;
        }
     
        /**
         * @return the selectedCompetence
         */
        public List<SelectItem> getSelectedCompetence() {
            return selectedCompetence;
        }
     
        /**
         * @param selectedCompetence the selectedCompetence to set
         */
        public void setSelectedCompetence(List<SelectItem> selectedCompetence) {
            this.selectedCompetence = selectedCompetence;
        }
     
    }
    Classe CompetenceID.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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package domaine;
     
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Embeddable;
     
    /**
     *
     * @author NHDF1037
     */
    @Embeddable
    public class CompetenceId implements Serializable{
        @Column(name="id_technicien")
        private String idTechnicien;
     
        @Column(name="id_technique")
        private int idTechnique;
     
     
        private int attribut1;
        private String attribut2;
        private boolean visible; //Attribut non-representatif et donc ignoré
     
        public CompetenceId(){
     
        }
     
        public CompetenceId(String idTechnicien, int idTechnique){
            this.idTechnicien = idTechnicien;
            this.idTechnique = idTechnique;
        }
     
        @Override
        public boolean equals(Object obj){
            //Verification de l'égalité des attributs
            if(obj==this){
                return true;
            }
            //Vérification du type de paramètre
            if (obj instanceof CompetenceId) {
                CompetenceId other = (CompetenceId) obj;
                if (this.attribut1 != other.attribut1) {
                    return false; // Les attributs sont différents
                }
                //Pour les attributs de type objets
                //On compare dans un premier temps les références
                if(!this.attribut2.equals(other.attribut2)){
                    //Si les références ne sont pas identiques
                    //On doit en plus utiliser equals()
                    if (this.attribut2 == null || !this.attribut2.equals(other.attribut2)) {
                        return false;
                    }
                }
                //Si on arrive ici c'est que tous les attributs sont égaux:
                return true;
            }
            return  false;
        }
     
        @Override
        public int hashCode() {
            int hash = 5;
            hash = 59 * hash + (this.idTechnicien != null ? this.idTechnicien.hashCode() : 0);
            hash = 59 * hash + this.idTechnique;
            return hash;
        }
    }
    Lorsque je lance une requete HQL pour afficher l'ensemble du contenu de la table Technicien, j'ai le champs technique qui apparait pour chaque technicien mais avec pour valeur : []

    Qu'en dites-vous ?

    Merci

  6. #6
    Membre Expert
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    1 348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 348
    Par défaut
    Dans ton equals, il y a une partie qui ne convient pas (les commentaires sont bons mais pas le code)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    if(!this.attribut2.equals(other.attribut2)){
                    //Si les références ne sont pas identiques
                    //On doit en plus utiliser equals()
                    if (this.attribut2 == null || !this.attribut2.equals(other.attribut2)) {
                        return false;
                    }
                }
    Je te laisse relire ça ^^

    En revanche, je doute que ce soit la raison du comportement. J'avoue que là je ne sais pas du tout, je ne fais jamais de table sans clé primaire simple auto-générée ... (à mon sens, mais certains pourraient ne pas être d'accord, c'est une bonne pratique de toujours avoir un auto-num comme id dans une table. Ensuite on peut toujours mettre une contrainte d'unicité sur le couple d'id qui fonctionnellement est l'identifiant)

  7. #7
    Membre Expert
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    1 348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 348
    Par défaut
    Désolé, pas eu le temps de te répondre hier (mets ici plutôt qu'en message privé).
    Le equals de ta classe CompetenceID devrait être comme ça :
    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
     
    @Override
        public boolean equals(Object obj){
            //Verification de l'égalité des références (non nécessaire mais optimise la comparaison)
            if(obj==this){
                return true;
            }
            //Vérification du type de paramètre
            if (obj instanceof CompetenceId) {
                CompetenceId other = (CompetenceId) obj;
                if (this.attribut1 != other.attribut1) {
                    return false; // Les attributs sont différents
                }
                //Pour les attributs de type objets
                //On regarde d'abord la nullité
                if(this.attribut2==null && other.attribut2!=null) {
                    return false; // Les attributs sont différents
                }
                return this.attribut2.equals(other.attribut2);
            }
            return  false;
        }
    Juste une question : pourquoi tu te bases sur attribut1 et attribut2 pour l'égalité et non sur technique et technicien ?

  8. #8
    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,

    Pour le moment ma fonction equals était un "essai" d'un tuto trouvée sur internet.
    J'ai encore du mal à comprendre totalement ses fonctionnalités.

    Tu me conseilles donc de remplacer attribut1 et 2 par Technicien et Technique ?

    J'essaye ça et je reviens vers toi,

    Merci bcp !!

  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
    Voici ma fonction equals :
    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
     
     @Override
        public boolean equals(Object obj){
            //Verification de l'égalité des références (non nécessaire mais optimise la comparaison)
            if(obj==this){
                return true;
            }
            //Vérification du type de paramètre
            if (obj instanceof CompetenceId) {
                CompetenceId other = (CompetenceId) obj;
                if (this.idTechnique != other.idTechnique) {
                    return false; // Les attributs sont différents
                }
                //Pour les attributs de type objets
                //On regarde d'abord la nullité
                if(this.idTechnicien==null && other.idTechnicien!=null) {
                    return false; // Les attributs sont différents
                }
                return this.idTechnicien.equals(other.idTechnicien);
            }
            return  false;
        }
    Voilà mais je n'arrive toujours pas à obtenir les données de ma table competenceV2 :


    J'ai moi même retiré les champs Nom, GSM...depuis le mappage j'ai la colonnes Techniques qui est arrivé mais taggé pour tous les techniciens par [], alors peut être cela correspond à la liste des compétences du techniciens sur les 300 produits ? (Chaque technicien a une fiche de compétence sur les 300 produits, une note par produit)

    Encore merci !!

  10. #10
    Membre Expert
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    1 348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 348
    Par défaut
    Ok pour ta nouvelle fonction equals.
    Les fonctions equals et hashcode sont nécessaires pour que le moteur soit capable de faire des ensemble d'éléments (les Set).
    Si tu n'as toujours pas les données qui remontent correctement, je te conseillerais de passer le driver de base de données et débug, pour qu'on puisse voir quelles sont les requêtes qui sont effectuées sur la base de données.
    Encore une fois, je ne suis pas trop à l'aise, n'ayant jamais manipulé les clés composites moi même.

  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
    Bon la situation semble bloquée,

    Je vais partir sur autre chose, j'ai trouvé ce post : http://www.developpez.net/forums/d88...ire-composite/

    Ca parait si simple, et cela correspond à mon problème de vouloir mapper une table avec clé composite !

    Qu'en pensez-vous ?

    Merci

  12. #12
    Membre Expert
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    1 348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 348
    Par défaut
    De ce que je lis là, tu t'es pas emmelé les pinceaux entre les deux possibilités ?
    http://docs.jboss.org/hibernate/anno...ingle/#d0e2177

  13. #13
    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
    En effet, je commence à me mélanger les pinceaux !
    J'essaye à nouveaux une méthode avec annotation, qui semble beaucoup plus fiable !
    Merci

  14. #14
    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 à tous,

    Encore et toujours dans mon problème de mapping, je viens de tout refaire, et cela ne fonctionne toujours pas, je vais donc essayer de vous mettre tous les éléments :

    Ma table competencev2 est une table de jonction entre Technique et Technicien. Elle contient les compétences de chacun des techniciens sur toutes les techniques.

    Table Technicien :


    Table Technique :


    Table CompetenceV2 :


    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
     
    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.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
     
     
     
    /**
     * Technicien generated by hbm2java
     */
    @Entity
    public class Technicien implements java.io.Serializable {
     
        @Id
        @Column(name="id_ft")
        private String idFt;
     
        @OneToMany(mappedBy="technicien")
        private Set<Competencev2> techniques = new HashSet<Competencev2>();
     
        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;
     
     
        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;
        }
     
        public String getIdFt() {
            return this.idFt;
        }
     
        public void setIdFt(String idFt) {
            this.idFt = idFt;
        }
     
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
     
        public Localisation getLocalisation() {
            return this.localisation;
        }
     
        public void setLocalisation(Localisation localisation) {
            this.localisation = localisation;
        }
     
        public Responsable getResponsable() {
            return this.responsable;
        }
     
        public void setResponsable(Responsable responsable) {
            this.responsable = responsable;
        }
     
        public String getNom() {
            return this.nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getPrenom() {
            return this.prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        public String getNumGsm() {
            return this.numGsm;
        }
     
        public void setNumGsm(String numGsm) {
            this.numGsm = numGsm;
        }
     
        public String getMobilite() {
            return this.mobilite;
        }
     
        public void setMobilite(String mobilite) {
            this.mobilite = mobilite;
        }
     
        public String getDateNaissance() {
            return this.dateNaissance;
        }
     
        public void setDateNaissance(String dateNaissance) {
            this.dateNaissance = dateNaissance;
        }
     
        public String getDepart() {
            return this.depart;
        }
     
        public void setDepart(String depart) {
            this.depart = depart;
        }
     
        /**
         * @return the techniques
         */
        public Set<Competencev2> getTechniques() {
            return techniques;
        }
     
        /**
         * @param techniques the techniques to set
         */
        public void setTechniques(Set<Competencev2> 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
     
    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.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
     
    /**
     * Technique generated by hbm2java
     */
    @Entity
    public class Technique  implements java.io.Serializable {
     
        @Id
        @Column(name="id_technique")
         private Integer idTechnique;
     
        @OneToMany(mappedBy="technique")
        private Set<Competencev2> techniciens = new HashSet<Competencev2>();
     
         private Produit produit;
         private Domaine domaine;
         private String categorieTechnique;
         private String nomTechnique;
     
     
        public Technique() {
        }
     
        public Technique(Produit produit, Domaine domaine, String categorieTechnique, String nomTechnique) {
           this.produit = produit;
           this.domaine = domaine;
           this.categorieTechnique = categorieTechnique;
           this.nomTechnique = nomTechnique;
        }
     
     
        public Integer getIdTechnique() {
            return this.idTechnique;
        }
     
        public void setIdTechnique(Integer idTechnique) {
            this.idTechnique = idTechnique;
        }
     
        public Produit getProduit() {
            return this.produit;
        }
     
        public void setProduit(Produit produit) {
            this.produit = produit;
        }
        public Domaine getDomaine() {
            return this.domaine;
        }
     
        public void setDomaine(Domaine domaine) {
            this.domaine = domaine;
        }
        public String getCategorieTechnique() {
            return this.categorieTechnique;
        }
     
        public void setCategorieTechnique(String categorieTechnique) {
            this.categorieTechnique = categorieTechnique;
        }
        public String getNomTechnique() {
            return this.nomTechnique;
        }
     
        public void setNomTechnique(String nomTechnique) {
            this.nomTechnique = nomTechnique;
        }
     
        /**
         * @return the techniciens
         */
        public Set<Competencev2> getTechniciens() {
            return techniciens;
        }
     
        /**
         * @param techniciens the techniciens to set
         */
        public void setTechniciens(Set<Competencev2> 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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package domaine;
     
    import java.io.Serializable;
    import java.util.List;
    import javax.faces.model.SelectItem;
    import javax.persistence.*;
     
    /**
     *
     * @author NHDF1037
     */
    //Annotation pour déclarer la table de la bd
    @Entity
    public class Competencev2 implements Serializable {
        @EmbeddedId
        private CompetenceId id;
     
        @Column(name="note")
        private String note;
     
        @ManyToOne
        @JoinColumn(name="id_technicien", insertable=false, updatable=false)
        private Technicien technicien;
     
        @ManyToOne
        @JoinColumn(name="id_technique", insertable=false, updatable=false)
        private Technique technique;
     
        private List<SelectItem> selectedCompetence;
     
        public Competencev2(){
     
        }
     
        public Competencev2(Technicien technicien, Technique technique,String note){
            this.technicien = technicien;
            this.technique = technique;
     
            technicien.getTechniques().add(this);
            technique.getTechniciens().add(this);
     
            this.id = new CompetenceId(technicien.getIdFt(), technique.getIdTechnique());
            this.note = note;
        }
     
        /**
         * @return the technicien
         */
        public Technicien getTechnicien() {
            return technicien;
        }
     
        /**
         * @return the technique
         */
        public Technique getTechnique() {
            return technique;
        }
     
        /**
         * @return the selectedCompetence
         */
        public List<SelectItem> getSelectedCompetence() {
            return selectedCompetence;
        }
     
        /**
         * @param selectedCompetence the selectedCompetence to set
         */
        public void setSelectedCompetence(List<SelectItem> selectedCompetence) {
            this.selectedCompetence = selectedCompetence;
        }
     
    }
    CompetenceId.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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package domaine;
     
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Embeddable;
     
    /**
     *
     * @author NHDF1037
     */
    @Embeddable
    public class CompetenceId implements Serializable{
        @Column(name="id_technicien")
        private String idTechnicien;
     
        @Column(name="id_technique")
        private int idTechnique;
     
     
        public CompetenceId(){
     
        }
     
        public CompetenceId(String idTechnicien, int idTechnique){
            this.idTechnicien = idTechnicien;
            this.idTechnique = idTechnique;
        }
     
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final CompetenceId other = (CompetenceId) obj;
            if ((this.idTechnicien == null) ? (other.idTechnicien != null) : !this.idTechnicien.equals(other.idTechnicien)) {
                return false;
            }
            if (this.idTechnique != other.idTechnique) {
                return false;
            }
            return true;
        }
     
        @Override
        public int hashCode() {
            int hash = 7;
            hash = 59 * hash + this.idTechnique;
            return hash;
        }
     
    }
    Voilà tous les éléments sont illustrés. J'ai suivi le tutoriel suivant :
    http://blogtechno.novediagroup.com/m...pplementaires/

    Lorsque que je test le mapping, "From Technicien" par exemple, le champs "Techniques" apparaît mais avec cette valeur : "[]"

    Voyez-vous une erreur ?

    Merci d'avance
    Fred

  15. #15
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    84
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 84
    Par défaut
    Bonjour , A votre place je vais coder à la main les relations entre les tables ,
    je te recommande ces tutos http://www.youtube.com/playlist?list=PL4AFF701184976B25

    tu commences à partir de tuto 13 .


  16. #16
    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 ensemein, ces tutos sont vraiment bien !

    Par contre, dommage qu'il n'y en ait pas sur mon problème de clé composite...

    Merci

Discussions similaires

  1. Mapping JPA OneToMany @ManyToMany
    Par kanebody dans le forum JPA
    Réponses: 2
    Dernier message: 23/08/2013, 17h45
  2. Mapping JPA avec Postgres
    Par zero.h dans le forum JPA
    Réponses: 1
    Dernier message: 15/05/2012, 13h30
  3. Mapping JPA : Composite Key
    Par garthos dans le forum JPA
    Réponses: 5
    Dernier message: 27/10/2011, 11h28
  4. jpa dans context j2ee et junit
    Par jmturc dans le forum JPA
    Réponses: 1
    Dernier message: 23/06/2009, 12h26
  5. [JPA] [EJB3] - Mapping et @ManyToOne erreur
    Par ®om dans le forum JPA
    Réponses: 29
    Dernier message: 04/03/2007, 23h31

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