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

Spring Web Java Discussion :

Refus d'enregistrement dans la base de données


Sujet :

Spring Web Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 3
    Points : 4
    Points
    4
    Par défaut Refus d'enregistrement dans la base de données
    bonjour à tous, afin de créer une application de gestion des mouvements au sein d'une structure précise, la modélisation uml suivant la méthode 2TUP m'a conduit au diagramme de classe suivant : https://drive.google.com/open?id=1L3...ZAq2iw1komu8Bm. Choisissant pour l’implémentation le framework spring j'ai donc produit les codes suivants :

    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
    package cm.mit.entities;
     
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonSetter;
     
    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
     
    @Entity
    public class Evenement implements Serializable {
        @Id @GeneratedValue
        private Long identifiant;
        private Date depart;
        private  int duree;
        private String motif;
     
        @OneToMany(mappedBy = "evt", cascade = { CascadeType.REMOVE })
        private Set<Accord> pers ;
     
        public Evenement() {
            pers = new HashSet<Accord>();
        }
     
        public Evenement(Date depart, int duree, String motif) {
            pers = new HashSet<Accord>();
            this.depart = depart;
            this.duree = duree;
            this.motif = motif;
        }
     
        public Date getDepart() {
            return depart;
        }
     
        public void setDepart(Date depart) {
            this.depart = depart;
        }
     
        public int getDuree() {
            return duree;
        }
     
        public void setDuree(int duree) {
            this.duree = duree;
        }
     
        public String getMotif() {
            return motif;
        }
     
        public void setMotif(String motif) {
            this.motif = motif;
        }
     
        public Long getIdentifiant() {
            return identifiant;
        }
     
        @JsonIgnore
        public Set<Accord> getPers() {
            return pers;
        }
     
        @JsonSetter
        public void setPers(Set<Accord> pers) {
            this.pers = pers;
        }
     
        public void setIdentifiant(Long identifiant) {
            this.identifiant = identifiant;
        }
    }
    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
    package cm.mit.entities;
     
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonSetter;
     
    import java.io.Serializable;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
     
    @Entity
    public class Personne implements Serializable{
     
        public Personne() {
     
        }
     
        public Personne(String cni) {
            this.cni = cni;
        }
     
        public Personne(String cni, String name, String surname, String country) {
            this.name = name;
            this.surname = surname;
            this.country = country;
            this.cni = cni;
        }
     
        public Personne(String name, String surname, String country) {
            this.name = name;
            this.surname = surname;
            this.country = country;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getSurname() {
            return surname;
        }
     
        public String getPhoto() {
            return photo;
        }
     
        public void setPhoto(String photo) {
            this.photo = photo;
        }
     
        public void setSurname(String surname) {
            this.surname = surname;
        }
     
        public String getCountry() {
            return country;
        }
     
        public void setCountry(String country) {
            this.country = country;
        }
     
        @JsonIgnore
        public Set<Telephone> getTelephones() {
            return telephones;
        }
     
        @JsonSetter
        public void setTelephones(Set<Telephone> telephones) {
            this.telephones = telephones;
        }
     
        public Date getNaiss() {
            return naiss;
        }
     
        public void setNaiss(Date naiss) {
            this.naiss = naiss;
        }
     
        @JsonIgnore
        public Set<Email> getMails() {
            return mails;
        }
     
        @JsonSetter
        public void setMails(Set<Email> mails) {
            this.mails = mails;
        }
     
        public String getCni() {
            return cni;
        }
     
        public void setCni(String cni) {
            this.cni = cni;
        }
     
        public char getSex() {
            return sex;
        }
     
        public void setSex(char sex) {
            this.sex = sex;
        }
     
        @JsonIgnore
        public Set<Accord> getEvt() {
            return evt;
        }
     
        @JsonSetter
        public void setEvt(Set<Accord> evt) {
            this.evt = evt;
        }
     
        @Id
        private String cni = null;
        private char sex = 'M';
        private String photo;
        @Temporal(TemporalType.DATE)
        private Date naiss;
        private String name;
        private String surname;
        private String country;
     
        @OneToMany(mappedBy = "personne", cascade = { CascadeType.ALL })
        private Set<Email> mails = new HashSet<Email>();
        @OneToMany(mappedBy = "pers", cascade = { CascadeType.REMOVE })
        private Set<Accord> evt = new HashSet<Accord>();
        @OneToMany(mappedBy = "pers", cascade = { CascadeType.ALL })
        private Set<Telephone> telephones = new HashSet<Telephone>();
    }
    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
    package cm.mit.entities;
     
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonSetter;
     
     
    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
     
    @Entity
    public class Utilisateur extends Personne implements Serializable {
        @Column(unique=true, insertable=true, updatable=true, nullable=false)
        private String login;
        private String password;
        private boolean etat = true;
        @Column(name = "dates")
        @Temporal(TemporalType.DATE)
        private Date date; // date d'entrée en service
        private String diplre ; // diplome récent
        private String exppro; //expérience professionnelle
     
        public Utilisateur(String cni, String password) {
            super(cni);
            this.password = password;
        }
     
        public Utilisateur() {
        }
     
        public boolean isEtat() {
            return etat;
        }
     
        public void setEtat(boolean etat) {
            this.etat = etat;
        }
     
        public String getLogin() {
            return login;
        }
     
        public void setLogin(String login) {
            this.login = login;
        }
     
        @JsonIgnore
        public String getPassword() {
            return password;
        }
     
        @JsonSetter
        public void setPassword(String password) {
            this.password = password;
        }
     
        @JsonIgnore
        public Set<Groupe> getGpes() {
            return gpes;
        }
     
        @JsonSetter
        public void setGpes(Set<Groupe> gpes) {
            this.gpes = gpes;
        }
     
        public Date getDate() {
            return date;
        }
     
        public void setDate(Date date) {
            this.date = date;
        }
     
        public String getDiplre() {
            return diplre;
        }
     
        public void setDiplre(String diplre) {
            this.diplre = diplre;
        }
     
        public String getExppro() {
            return exppro;
        }
     
        public void setExppro(String exppro) {
            this.exppro = exppro;
        }
     
        @JsonIgnore
        public Set<Accord> getAccords() {
            return accords;
        }
     
        @JsonSetter
        public void setAccords(Set<Accord> accords) {
            this.accords = accords;
        }
     
        @ManyToMany(cascade = {CascadeType.PERSIST}, fetch = FetchType.EAGER)
        @JoinTable(name = "usergroup", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "GROUPE_ID"))
        private Set<Groupe> gpes = new HashSet<Groupe>();
     
        @OneToMany(mappedBy = "user", cascade = { CascadeType.ALL })
        private Set<Accord> accords = new HashSet<Accord>();
     
     
    }
    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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package cm.mit.entities;
     
    import org.hibernate.annotations.NotFound;
    import org.hibernate.annotations.NotFoundAction;
     
    import java.io.Serializable;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
     
     
    @Entity
    public class Email implements Serializable{
        @Id
        private String value = "  @  ";
     
        public Email() {
        }
     
        public Email(String email) {
            this.value = email;
        }
     
        public Email(String value, Personne personne) {
            this.value = value;
            personne.getMails().add(this);
            this.personne = personne;
        }
     
        public String getValue() {
            return value;
        }
     
        public void setValue(String value) {
            this.value = value;
        }
     
        public Personne getPersonne() {
            return personne;
        }
     
        public void setPersonne(Personne personne) {
            this.personne = personne;
        }
     
        @ManyToOne(fetch=FetchType.LAZY)
        @NotFound(action = NotFoundAction.IGNORE)
        @JoinColumn(name = "num_cni", nullable = false)
        private Personne personne;
    }
    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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package cm.mit.entities;
     
    import org.hibernate.annotations.NotFound;
    import org.hibernate.annotations.NotFoundAction;
     
    import java.io.Serializable;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
     
    /**
     *
     * @author major
     */
    @Entity
    public class Telephone implements Serializable{
        @Id
        private long numero = -1;
     
        public Telephone() {
     
        }
     
        public Telephone(long numero) {
            if(numero > 0)
                this.numero = numero;
        }
        public Telephone(Personne pers, long numero) {
            if(numero > 0) {
                this.numero = numero;
                pers.getTelephones().add(this);
                this.pers = pers;
            }
            else
                pers = null;
        }
     
     
        public long getNumero() {
            return numero;
        }
     
        public void setNumero(long numero) {
            if(numero > 0)
                this.numero = numero;
        }
     
        public Personne getPers() {
            return pers;
        }
     
        public void setPers(Personne pers) {
            this.pers = pers;
        }
     
        @ManyToOne(fetch=FetchType.LAZY)
        @NotFound(action = NotFoundAction.IGNORE)
        @JoinColumn(name = "pers_id", nullable = false)
        private Personne pers ;
    }
    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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package cm.mit.entities;
     
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonSetter;
     
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.*;
     
    /**
     *
     * @author major
     */
    @Entity
    public class Groupe implements Serializable{
        @Id @GeneratedValue
        @Column(name = "cle")
        private Long identifiant;
        private String name;
     
        public Groupe() {
        }
     
        public Groupe(String name) {
            this.name = name;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public Long getIdentifiant() {
            return identifiant;
        }
     
        public void setIdentifiant(Long identifiant) {
            this.identifiant = identifiant;
        }
     
        @JsonIgnore
        public Set<Utilisateur> getUser() {
            return user;
        }
     
        @JsonSetter
        public void setUser(Set<Utilisateur> user) {
            this.user = user;
        }
     
        @ManyToMany(mappedBy = "gpes")
        private Set<Utilisateur> user = new HashSet<Utilisateur>();
    }
    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
    package cm.mit.entities;
     
     
    import org.hibernate.annotations.NotFound;
    import org.hibernate.annotations.NotFoundAction;
     
    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Date;
     
    @Entity
    public class Accord {
        @Embeddable
        public static class Id implements Serializable {
            @Column(name = "evt_id")
            private Long evtId;
     
            @Column(name = "pers_id")
            private String persId;
     
            @GeneratedValue
            private Long incre;
     
            public Id() {
            }
     
            public Id(Long evtId, String persId) {
                this.evtId = evtId;
                this.persId = persId;
            }
     
            public Long getEvtId() {
                return evtId;
            }
     
            public void setEvtId(Long evtId) {
                this.evtId = evtId;
            }
     
            public String getPersId() {
                return persId;
            }
     
            public void setPersId(String persId) {
                this.persId = persId;
            }
     
            public Long getIncre() {
                return incre;
            }
     
            public void setIncre(Long incre) {
                this.incre = incre;
            }
        }
     
        public Accord() {
            id = new Id();
        }
     
        public Accord(Utilisateur user) {
            id = new Id();
            user.getAccords().add(this);
            this.user = user;
        }
     
        public Accord(Personne pers, Evenement evt, Utilisateur user) {
            id = new Id(evt.getIdentifiant(),pers.getCni());
           /* this.setPers(pers);
            this.setEvt(evt);*/
            pers.getEvt().add(this);
            evt.getPers().add(this);
            this.pers = pers;
            this.evt = evt;
            user.getAccords().add(this);
            this.user = user;
        }
     
     
        public Accord(boolean decision) {
            this.decision = decision;
        }
     
        public boolean isDecision() {
            return decision;
        }
     
        public void setDecision(boolean decision) {
            this.decision = decision;
        }
     
        public Date getFaitle() {
            return faitle;
        }
     
        public void setFaitle(Date faitle) {
            this.faitle = faitle;
        }
     
        public String getPiecejointe() {
            return piecejointe;
        }
     
        public void setPiecejointe(String piecejointe) {
            this.piecejointe = piecejointe;
        }
     
        public int getDuree() {
            return duree;
        }
     
        public void setDuree(int duree) {
            this.duree = duree;
        }
     
        public Id getId() {
            return id;
        }
     
        public void setId(Id id) {
            this.id = id;
        }
     
        public Personne getPers() {
            return pers;
        }
     
        public void setPers(Personne pers) {
            this.pers = pers;
        }
     
        public Evenement getEvt() {
            return evt;
        }
     
        public void setEvt(Evenement evt) {
            this.evt = evt;
        }
     
        public Utilisateur getUser() {
            return user;
        }
     
        public void setUser(Utilisateur user) {
            this.user = user;
        }
     
        private boolean decision = false ;
        private Date faitle;
        private String piecejointe;
        private int duree;
        @EmbeddedId
        private Id id ;
        @ManyToOne(fetch=FetchType.LAZY)
        @NotFound(action = NotFoundAction.IGNORE)
        @JoinColumn(name = "user_id", nullable = false)
        private Utilisateur user;
        @ManyToOne
        @NotFound(action = NotFoundAction.IGNORE)
        @JoinColumn(name = "pers_id", insertable = false, updatable = false)
        private Personne pers = new Personne();
        @ManyToOne()
        @NotFound(action = NotFoundAction.IGNORE)
        @JoinColumn(name = "evt_id", insertable = false, updatable = false)
        private Evenement evt = new Evenement();
     
    }
    contenu du fichier pom.xml
    Code XML : 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
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.1.8.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    	<groupId>cm.mit</groupId>
    	<artifactId>fingerprints</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>fingerprints</name>
    	<description>Demo project for Spring Boot</description>
     
    	<properties>
    		<java.version>1.8</java.version>
    		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    	</properties>
     
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-actuator</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-jpa</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-rest</artifactId>
    		</dependency>
    		<!--<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-security</artifactId>
    		</dependency>-->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!--<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-config</artifactId>
    		</dependency>-->
    		<!--<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    		</dependency>-->
     
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<scope>runtime</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<!--<dependency>
    			<groupId>org.springframework.security</groupId>
    			<artifactId>spring-security-test</artifactId>
    			<scope>test</scope>
    		</dependency>-->
    	</dependencies>
     
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${spring-cloud.version}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
     
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
     
    </project>

    fichier application.properties

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    spring.datasource.url = jdbc:mysql://localhost:3306/mit_soft?serverTimezone=UTC
    spring.datasource.username = root
    spring.datasource.password = mon_mot_de_passe
    spring.jpa.hibernate.ddl-auto = update
    #spring.datasource.driverClassName = com.mysql.jdbc.Driver
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    Partie Dao :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package cm.mit.dao;
     
    import cm.mit.entities.Personne;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
     
    @RepositoryRestResource
    public interface PersonneRepository extends JpaRepository<Personne, String> {
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package cm.mit.dao;
     
    import cm.mit.entities.Utilisateur;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
     
    @RepositoryRestResource
    public interface UtilisateurRepository extends JpaRepository<Utilisateur, String> {
     
    }
    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
    package cm.mit.dao;
     
    import cm.mit.entities.Evenement;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
     
    @RepositoryRestResource
    public interface EvenementRepository extends JpaRepository<Evenement , Long> {
        @Query("select evt from Evenement evt where evt.motif like:x")
        public Page<Evenement> recherche(@Param("x") String mot , Pageable page);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package cm.mit.dao;
     
    import cm.mit.entities.Email;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
     
    @RepositoryRestResource
    public interface EmailRepository extends JpaRepository<Email , String > {
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package cm.mit.dao;
     
    import cm.mit.entities.Telephone;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
     
    @RepositoryRestResource
    public interface TelephoneRepository extends JpaRepository<Telephone, Long> {
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package cm.mit.dao;
     
    import cm.mit.entities.Groupe;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
     
    @RepositoryRestResource
    public interface GroupeRepository extends JpaRepository<Groupe , Long> {
    }
    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
    package cm.mit.dao;
     
    import cm.mit.entities.Accord;
    import cm.mit.entities.Accord.Id;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;
     
     
    public interface AccordRepository extends JpaRepository<Accord, Id> {
        @Query("select a from Accord a where a.id.persId = x")
        public Page<Accord>result(@Param("x") Long pers_id , Pageable page);
    }
    section web service
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    package cm.mit.usecase;
     
    import cm.mit.dao.*;
    import cm.mit.entities.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.web.bind.annotation.*;
     
    @RestController
    public class Circuit {
     
        //demander congés
        @RequestMapping(value = "/askholiday/{id}/{key}", method = RequestMethod.POST)
        public Boolean askholiday(@RequestBody Accord a , @PathVariable("id") String id , @PathVariable("key") Long key)
        {
            if((pers.existsById(id)&&(evt.existsById(key)))){
                Personne p = pers.getOne(id);
                System.out.println("---- avant la création -----"+ p.getEvt().size());
                Evenement e = evt.getOne(key);
                Accord joint = new Accord(p,e,a.getUser());
                joint.setPiecejointe(a.getPiecejointe());
                System.out.println("-----aprés la création -----"+ p.getEvt().size());
                evt.saveAndFlush(e);
                System.out.println("-----resultat opération -----1111111111111");
                pers.saveAndFlush(p);
                System.out.println("-----resultat opération -----222222222222222");
                user.saveAndFlush(a.getUser());
                System.out.println("-----resultat opération -----3333333333333333");
                rep.save(joint);
                return true;
            }
            else
                return false;
        }
     
        //recevoir reponse
        @RequestMapping(value = "/viewholiday/{id}", method = RequestMethod.GET)
        public Page<Accord> rapport (@PathVariable("id") Long id , @RequestParam(name = "page", defaultValue = "0") int page , @RequestParam(name = "size", defaultValue = "10") int size){
            return rep.result(id , new PageRequest(page,size));
        }
     
        //consulter demande de congés
        @RequestMapping(value = "/accord", method = RequestMethod.GET)
        public Page<Accord> listAccord (@RequestParam(name = "page", defaultValue = "0") int page , @RequestParam(name = "size", defaultValue = "10") int size){
            return rep.findAll(new PageRequest(page,size));
        }
     
        //update holidays
        @RequestMapping(value = "/upholiday", method = RequestMethod.POST)
        public boolean update(@RequestBody Accord value , @RequestBody Evenement e){
            Accord recu = rep.getOne(value.getId());
            if(recu == null)
                return false;
            recu.setPiecejointe(value.getPiecejointe());
            rep.saveAndFlush(recu);
            Evenement mod = e;
            mod.setIdentifiant(value.getId().getEvtId());
            if(evt.saveAndFlush(mod) == null)
                return false;
            return  true;
        }
     
        //delcongés
        @RequestMapping(value = "/accord/{id}", method = RequestMethod.DELETE)
        public void delete(@PathVariable("id") Accord.Id id ){
            rep.deleteById(id);
        }
     
        @RequestMapping(value = "/accord", method = RequestMethod.DELETE)
        public void delete2(@RequestBody Accord.Id id ){ rep.deleteById(id); }
     
        @RequestMapping(value = "/delaccord", method = RequestMethod.DELETE)
        public void delete3(@RequestBody Accord elt ){
            rep.deleteById(elt.getId());
        }
     
        //traiter demande de congés
        @RequestMapping(value = "/traiterholiday", method = RequestMethod.POST)
        public boolean traitement(@RequestBody Accord value ){
            Accord recu = rep.getOne(value.getId());
            if(recu == null)
                return false;
            recu.setPiecejointe(value.getPiecejointe());
            rep.saveAndFlush(recu);
            return  true;
        }
     
     
     
        //repondre demande de congés
        @RequestMapping(value = "/accord", method = RequestMethod.PUT)
        public boolean traiteur(@RequestBody Accord value ){
            rep.saveAndFlush(value);
            return  true;
        }
     
        @RequestMapping(value = "/find", method = RequestMethod.GET)
        public Page<Evenement> find(String elt, @RequestParam(name = "page", defaultValue = "0") int page , @RequestParam(name = "size", defaultValue = "10") int size){
            return  evt.recherche(elt , new PageRequest(page,size));
        }
     
        @RequestMapping(value = "/utilisateur/{id}", method = RequestMethod.PUT)
        public boolean utilisateurs(@PathVariable("id") String id , @RequestBody Utilisateur elt){
            if (!pers.existsById(id)) {
                elt.setCni(id);
                user.saveAndFlush(elt);
            }
            else
                elt.setCni(id);
                pers.deleteById(id);
                user.saveAndFlush(elt);
            return  true;
        }
     
        @RequestMapping(value = "/utilisateur", method = RequestMethod.POST)
        public boolean adduser(@RequestBody Utilisateur elt){
            if (!pers.existsById(elt.getCni())){
                if(!user.existsById(elt.getCni()))
                    user.saveAndFlush(elt);
                    return true;
            }
            else {
                if (user.existsById(elt.getCni()))
                    return false ;
                Personne test = pers.getOne(elt.getCni());
                elt.setName(test.getName());
                elt.setSurname(test.getSurname());
                elt.setSex(test.getSex());
                elt.setNaiss(test.getNaiss());
                elt.setCountry(test.getCountry());
                elt.setPhoto(test.getPhoto());
                elt.setTelephones(test.getTelephones());
                elt.setMails(test.getMails());
                elt.setEvt(test.getEvt());
                pers.deleteById(elt.getCni());
                user.save(elt);
                return true;
            }
        }
     
        @RequestMapping(value = "/utilisateur/{id}", method = RequestMethod.DELETE)
        public boolean del(@PathVariable("id") String id ){
            if (user.existsById(id)){
                Personne pers = user.getOne(id);
                user.deleteById(id);
                this.pers.saveAndFlush(pers);
                return  true;
            }
            else
                return false;
     
        }
     
        //consulter liste utilisateur
        @RequestMapping(value = "/utilisateurs", method = RequestMethod.GET)
        public Page<Utilisateur> viewuser (@RequestParam(name = "page", defaultValue = "0") int page , @RequestParam(name = "size", defaultValue = "10") int size){
            return user.findAll(new PageRequest(page,size));
        }
     
     
        // section telephone
        @RequestMapping(value = "/telephone", method = RequestMethod.POST)
        public Telephone ajoutnumber(@RequestBody Telephone tel){
            String id = tel.getPers().getCni();
            if(pers.existsById(id)){
                Personne persf = pers.getOne(id);
                persf.getTelephones().add(tel);
                pers.saveAndFlush(persf);
                return basephone.save(new Telephone(persf,tel.getNumero()));
            }
            else
                return null;
        }
     
        // section email
        @RequestMapping(value = "/email", method = RequestMethod.POST)
        public Email addmail(@RequestBody Email mail){
            String id = mail.getPersonne().getCni();
            if(pers.existsById(id)){
                Personne persf = pers.getOne(id);
                persf.getMails().add(mail);
                pers.saveAndFlush(persf);
                return emails.save(new Email(mail.getValue(),persf));
            }
            else
                return null;
        }
     
     
        @Autowired
        private PersonneRepository pers;
        @Autowired
        private EvenementRepository evt;
        @Autowired
        private AccordRepository rep;
        @Autowired
        private UtilisateurRepository user;
        @Autowired
        private TelephoneRepository basephone;
        @Autowired
        private EmailRepository emails;
    }
    Une fois le code terminé, l'application exécutée et la base de données MySQL démarrée, à l'aide de Advanced REST client, je crée via les requêtes http les éléments dans la base de données. Certaines requêtes déclenchent le code erreur 500 tout en enregistrant les données dans le sgbd. par exemple lorsque je lance url http://localhost:8080/email avec le contenu json (ici on suppose que l'identifiant 2019 est déjà dans la base de données)
    Code JSON : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    {
      "value" : "test@yahoo.fr",
      "personne" : {"cni" : "2019"}
    }

    l'email est bien créé dans la base mais le message retourné est le suivant :
    Code JSON : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    {
    "timestamp": "2019-09-21T20:24:53.569+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "A different object with the same identifier value was already associated with the session : [cm.mit.entities.Email#test@yahoo.fr]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [cm.mit.entities.Email#test@yahoo.fr]",
    "path": "/email"
    }

    dans le cas de l'url http://localhost:8080/askholiday/2018/1, avec comme exemple le contenu json
    Code JSON : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    {
      "piecejointe" : "/home/major/document/new.docx",
      "user" : {"cni" : "2019"}
    }

    nous avons un message d'erreur :
    Code JSON : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    {
    "timestamp": "2019-09-21T20:40:38.119+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Unable to find cm.mit.entities.Accord with id cm.mit.entities.Accord$Id@55a36887; nested exception is javax.persistence.EntityNotFoundException: Unable to find cm.mit.entities.Accord with id cm.mit.entities.Accord$Id@55a36887",
    "path": "/askholiday/2018/1"
    }

    dans la base de données aucune info n'est ajoutée.

    SVP aidez-moi, certaines méthodes "post" produites par l'annotation @RepositoryRestResource ne marchent pas sur les classes qui au moment de la conversion en modelé relationnelle possèdent des clés étrangères.

  2. #2
    Membre confirmé Avatar de Kazh Du
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2011
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2011
    Messages : 152
    Points : 561
    Points
    561
    Par défaut
    Pour le ws /email (je n'ai pas regardé l'autre mais le problème est sûrement similaire) :
    Tu commences par ajouter le mail dans la classe personne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Personne persf = pers.getOne(id);
    persf.getMails().add(mail);
    pers.saveAndFlush(persf);
    Ce qui créé un nouvel email dans ta base à cause du cascade.ALL :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    @OneToMany(mappedBy = "personne", cascade = { CascadeType.ALL })
    private Set<Email> mails = new HashSet<Email>();
    puis tu crées à nouveau un email
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    emails.save(new Email(mail.getValue(),persf));
    or la valeur sert de clé primaire et donc plantage.

    Pour éviter ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Personne persf = pers.getOne(id);
    return emails.save(new Email(mail.getValue(),persf));
    Par contre, honnêtement, ton code est mal structuré. Il faut ajouter des classes services entre tes contrôleurs et tes repos. Les classes services doivent gérer les erreurs des repos.
    Ensuite : 1 contrôleur = 1 service = 1 repository.
    Merci d'ajouter un sur les tags qui vous ont aidé

Discussions similaires

  1. enregistrement dans la base de donnée
    Par car00x dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 03/11/2006, 18h28
  2. Réponses: 1
    Dernier message: 30/07/2006, 20h02
  3. enregistrer dans une base de données
    Par liverbird dans le forum C++Builder
    Réponses: 33
    Dernier message: 12/07/2006, 19h50
  4. [MySQL] Enregistrer dans une base de données à partir d'une liste déroulante
    Par snakejl dans le forum PHP & Base de données
    Réponses: 12
    Dernier message: 17/05/2006, 16h32
  5. inscription via le web et enregistrement dans une base de données
    Par titoenis dans le forum Balisage (X)HTML et validation W3C
    Réponses: 11
    Dernier message: 17/05/2006, 09h20

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