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 :

Probleme d'insertion sur hibernate


Sujet :

Hibernate Java

  1. #1
    Membre du Club
    Homme Profil pro
    éléve ingénieur
    Inscrit en
    Mai 2015
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : éléve ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2015
    Messages : 75
    Points : 57
    Points
    57
    Par défaut Probleme d'insertion sur hibernate
    Bonjour tout le monde

    je travaille avec hibernate + jsf et mysql comme sgbd et j'ai un petit problème de l'insertion , en fait sur la classe personnel j'ai deux champs agence et statut de type agence et statut .
    j'arrive pas à insérer dans la table personnel . aider moi s'il vous plait à résoudre ce probleme

    voici la class personnel

    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
     
     
    @Entity
    @Table(name="personnel"
        ,catalog="derichebourg"
    )
    public class Personnel  implements java.io.Serializable {
     
     
         private String cin;
         private Statut statut;
         private Agence agence;
         private String nomPersonnel;
         private String prenom;
         private String numTelephone;
         private String adresse;
         private String fonction;
         private String login;
         private String motDePasse;
         private String mail;
         private Set<Conducteur> conducteurs = new HashSet<Conducteur>(0);
         private Set<Responsableconducteur> responsableconducteurs = new HashSet<Responsableconducteur>(0);
     
        public Personnel() {
        }
     
     
        public Personnel(String cin, Statut statut, Agence agence) {
            this.cin = cin;
            this.statut = statut;
            this.agence = agence;
        }
        public Personnel(String cin, Statut statut, Agence agence, String nomPersonnel, String prenom, String numTelephone, String adresse, String fonction, String login, String motDePasse, String mail, Set<Conducteur> conducteurs, Set<Responsableconducteur> responsableconducteurs) {
           this.cin = cin;
           this.statut = statut;
           this.agence = agence;
           this.nomPersonnel = nomPersonnel;
           this.prenom = prenom;
           this.numTelephone = numTelephone;
           this.adresse = adresse;
           this.fonction = fonction;
           this.login = login;
           this.motDePasse = motDePasse;
           this.mail = mail;
           this.conducteurs = conducteurs;
           this.responsableconducteurs = responsableconducteurs;
        }
     
     
     
     
         @Id 
     
        @Column(name="cin", unique=true, nullable=false, length=700)
        public String getCin() {
            return this.cin;
        }
     
        public void setCin(String cin) {
            this.cin = cin;
        }
    @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="statut", nullable=false)
        public Statut getStatut() {
            return this.statut;
        }
     
        public void setStatut(Statut statut) {
            this.statut = statut;
        }
    @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="numAgence", nullable=false)
        public Agence getAgence() {
            return this.agence;
        }
     
        public void setAgence(Agence agence) {
            this.agence = agence;
        }
     
        @Column(name="nomPersonnel", length=700)
        public String getNomPersonnel() {
            return this.nomPersonnel;
        }
     
        public void setNomPersonnel(String nomPersonnel) {
            this.nomPersonnel = nomPersonnel;
        }
     
        @Column(name="prenom", length=700)
        public String getPrenom() {
            return this.prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        @Column(name="numTelephone", length=500)
        public String getNumTelephone() {
            return this.numTelephone;
        }
     
        public void setNumTelephone(String numTelephone) {
            this.numTelephone = numTelephone;
        }
     
        @Column(name="adresse", length=900)
        public String getAdresse() {
            return this.adresse;
        }
     
        public void setAdresse(String adresse) {
            this.adresse = adresse;
        }
     
        @Column(name="fonction", length=700)
        public String getFonction() {
            return this.fonction;
        }
     
        public void setFonction(String fonction) {
            this.fonction = fonction;
        }
     
        @Column(name="login", length=700)
        public String getLogin() {
            return this.login;
        }
     
        public void setLogin(String login) {
            this.login = login;
        }
     
        @Column(name="mot_de_passe", length=900)
        public String getMotDePasse() {
            return this.motDePasse;
        }
     
        public void setMotDePasse(String motDePasse) {
            this.motDePasse = motDePasse;
        }
     
        @Column(name="mail", length=500)
        public String getMail() {
            return this.mail;
        }
     
        public void setMail(String mail) {
            this.mail = mail;
        }
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="personnel")
        public Set<Conducteur> getConducteurs() {
            return this.conducteurs;
        }
     
        public void setConducteurs(Set<Conducteur> conducteurs) {
            this.conducteurs = conducteurs;
        }
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="personnel")
        public Set<Responsableconducteur> getResponsableconducteurs() {
            return this.responsableconducteurs;
        }
     
        public void setResponsableconducteurs(Set<Responsableconducteur> responsableconducteurs) {
            this.responsableconducteurs = responsableconducteurs;
        }
     
    }

    et la méthode ajouter sur DAO :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
     
          /* Ajouter un personnel */
     
        public void create(Personnel p){
            Session s = HibernateUtil.getSessionFactory().getCurrentSession();
     
            try{
                s.beginTransaction();
                s.save(p);
                s.getTransaction().commit();
            }catch(Exception e){
                e.printStackTrace();
                s.getTransaction().rollback();
            }
     
        }


    et la méthode insert sur personnelController.java :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     
       public String insert () {
     
             personnelDAO dao = new personnelDAO() ;
             dao.create(p); 
             return "succes" ;
        }




    et sur la vue :

    NB: show = personnelController.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
     
     
    <h:form>
         <h:panelGrid columns="2">
     
             <h:outputText value="Cin"/>
             <h:inputText label="Cin" id="Cin"  value="#{show.p.cin}">  
                    </h:inputText>
     
     
                    <h:outputText value="Agence"/>
                    <h:inputText label="agence" id="agence"  value="#{show.p.agence.numAgence}">
     
                    </h:inputText>
     
                    <h:outputText value="Statut"/>
                    <h:inputText label="statut" id="statut"  value="#{show.p.statut.numStatut}">
     
                    </h:inputText>
     
     
                    <h:outputText value="Nom"/>
                    <h:inputText label="nom" id="nom" value="#{show.p.nomPersonnel}">
     
                    </h:inputText>
     
                    <h:outputText value="Prenom:" />
                    <h:inputText label="prenom" id="prenom"  value="#{show.p.prenom}">
     
                    </h:inputText>
     
                    <h:outputText value="Num :" />
                    <h:inputText label="pum" id="num"  value="#{show.p.numTelephone}">
     
                    </h:inputText>
     
                    <h:outputText value="Fonction:" />
                    <h:inputText label="fonction" id="fonction"  value="#{show.p.fonction}">
     
                    </h:inputText>
     
                     <h:outputText value="Login :" />
                     <h:inputText label="login" id="login"  value="#{show.p.login}">
     
                    </h:inputText>
     
                       <h:outputText value=" passe :" />
                       <h:inputText label="passe" id="passe"  value="#{show.p.motDePasse}">
     
                    </h:inputText>
     
                          <h:outputText value="Adresse :" />
                          <h:inputText label="adresse" id="adresse"  value="#{show.p.adresse}">
     
                    </h:inputText>
     
                             <h:outputText value=" Mail :" />
                             <h:inputText label="mail" id="mail" value="#{show.p.mail}">
     
                    </h:inputText>
     
                    <f:facet name="footer">
                        <a4j:commandButton value="Ajouter"  action="#{show.insert()}" execute="popup" />
                    </f:facet>
                </h:panelGrid>
            </h:form>
        </rich:popupPanel>
     
        </h:form>

  2. #2
    Membre à l'essai
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2015
    Messages : 16
    Points : 23
    Points
    23
    Par défaut Insertion
    Je ne suis pas certain mais quand tu parles d'insérer les champs Agence et Statut dans la table Personnel tu parles des Id respectifs de ces entrées.
    Tu as bien dans ta table personnel 2 clef étrangères agence_id et statut_id pointant respectivement vers agence.id et statut.id éventuellement avec ondelete Cascade et onupdate Cascade ?

    Si oui sélectionner les agences et les statut avec un inputText n'est peut être pas la meilleure solution.
    Agence et Statut sont des objets et doivent donc être traités comme tels. Un inputText te permet de saisir un String.
    Il n'y a donc pas de concordance de type.

    A ta place j'utiliserait un composant selectOneMenu. C'est une drop box qui te permet de choisir ton Agence ou ton Statut.
    Tu devra donc créer 2 contrôleurs supplémentaires et 2 EJB (DAO) supplémentaires - un pour chaque entité.
    Il faudra aussi y intégrer un convertisseur. Pour convertir le string saisi dans le selectOneMenu en un objet de type Agence ou de type Statut.

    Tes utilisateurs ne pourront ainsi pas saisir de statut ou d'agence manuellement ce qui évite les redondances dans la base de données et les fautes de frappe.

    J'espère que ça va t'aider un peu

  3. #3
    Membre du Club
    Homme Profil pro
    éléve ingénieur
    Inscrit en
    Mai 2015
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : éléve ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2015
    Messages : 75
    Points : 57
    Points
    57
    Par défaut
    Bonjour

    Tu as bien dans ta table personnel 2 clef étrangères agence_id et statut_id pointant respectivement vers agence.id et statut.id éventuellement avec ondelete Cascade et onupdate Cascade ?

    Pour les clés étrangers agence.id et statut.id sont avec : on update restrict on delete restrict .

    Tu devra donc créer 2 contrôleurs supplémentaires et 2 EJB (DAO) supplémentaires - un pour chaque entité.

    j'ai deja crée cela pour avoir la liste des agences et statuts et ca marche bien dans les selectOneMenu , j'ai pu afficher les noms des agences et les noms des statuts mais c'est pas ça le probleme .

    Il faudra aussi y intégrer un convertisseur. Pour convertir le string saisi dans le selectOneMenu en un objet de type Agence ou de type Statut.

    ici ou il y a le probleme je ne sais pas comment faire , et à ton avis la méthode create que j'ai implémenté sur personnelDAO est correcte ou non ?

    ========================================================================================

  4. #4
    Membre à l'essai
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2015
    Messages : 16
    Points : 23
    Points
    23
    Par défaut Méthode save
    A première vue oui la méthode à l'air correcte.
    Si tu as un fichier persistence.xml tu peux mettre
    <properties>
    <property name="eclipselink.logging.level.sql" value="FINE"/>
    <property name="eclipselink.logging.parameters" value="true"/>
    </properties>

    Pour tracer tes accès DB tu pourra voir le code SQL avec les insert passer.


    Pour les convertisseurs voici un exemple à mettre à la fin de ton contrôleur

    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
     @FacesConverter(forClass = People.class)
        public static class PeopleControlerConverter implements Converter {
     
            @Override
            public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
                if (value == null || value.length() == 0) {
                    return null;
                }
                try {
                    PeopleControler controler = (PeopleControler) facesContext.getApplication().getELResolver().
                            getValue(facesContext.getELContext(), null, "peopleCtrl");
                    return controler.getPeopleEJB().find(getKey(value));
                } catch (NumberFormatException e) {
                    throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid theme."));
                }
            }
     
            java.lang.Long getKey(String value) {
                java.lang.Long key;
                key = Long.valueOf(value);
                return key;
            }
     
            String getStringKey(java.lang.Long value) {
                StringBuilder sb = new StringBuilder();
                sb.append(value);
                return sb.toString();
            }
     
            @Override
            public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
                if (object == null) {
                    return null;
                }
                if (object instanceof People) {
                    People o = (People) object;
                    return getStringKey(o.getId());
                } else {
                    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "object {0} is of type {1}; expected type: {2}", new Object[]{object, object.getClass().getName(), People.class.getName()});
                    return null;
                }
            }
        }

  5. #5
    Membre du Club
    Homme Profil pro
    éléve ingénieur
    Inscrit en
    Mai 2015
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : éléve ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2015
    Messages : 75
    Points : 57
    Points
    57
    Par défaut
    Bonjour

    En ce qui concerna le convertisseur que vous m'avez donné , qu'est ce que je dois mettre à la place de PeopleControler ?

    est ce que ma classe personnel ?

  6. #6
    Membre à l'essai
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2015
    Messages : 16
    Points : 23
    Points
    23
    Par défaut
    Normalement tu dois mettre PersonnelControler ou le nom du contrôleur que tu utilise qui est associé a ton entité personnel

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 8
    Dernier message: 23/05/2013, 09h28
  2. Probleme de lock sur un trigger after insert
    Par funboard dans le forum Oracle
    Réponses: 5
    Dernier message: 04/03/2009, 12h14
  3. probleme d'insertion flash sur mon site
    Par chills dans le forum Intégration
    Réponses: 1
    Dernier message: 22/08/2008, 00h25
  4. [Debutant]Probleme d'insert sur sequence !
    Par Tchinkatchuk dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 19/04/2005, 15h37
  5. probleme d'installation sur d'autres postes
    Par VBkiller dans le forum Composants VCL
    Réponses: 4
    Dernier message: 18/09/2002, 18h14

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