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 avec suppression en cascade


Sujet :

Hibernate Java

  1. #1
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    167
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 167
    Points : 85
    Points
    85
    Par défaut Probleme avec suppression en cascade
    Bonjour,

    Alors j'ai mit en oeuvre les annotations de type cascade pour supprimer en cascade tout ce qui est relié, dans mon cas, a la table "Person".

    La suppression en cascade marche sauf pour deux relations avec deux autres tables qui sont toutes les deux de types @ManyToOne et @OneToMany

    Meme en mettant du @OneToOne ca ne marchait pas donc voila je dois probablement faire une erreur, bien que c'est etrangre vu qu'avec le reste ca fonctionne.

    Voici le code de mon entité "Person" :

    Person.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    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
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package jim.sums.db;
     
    import javax.persistence.CascadeType;
    import jim.common.jimaas.struts2.User;
    import java.io.Serializable;
    import java.sql.Timestamp;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
     
    /**
     *
     * @author BreyA
     */
    @Entity
    public class Person implements Serializable, User {
     
        private static final long serialVersionUID = 1L;
        private Long id;
        private String forename;
        private String surname;
        private String username;
        private String password;
        private Timestamp creationDate;
        private Timestamp confirmationDate;
        private Timestamp lastLogin;
        private Set<Cohort> cohort = new HashSet<Cohort>();
        private Set<Email> email = new HashSet<Email>();
        private Organisation organisation;
        private PersonStatus status;
        private Set<Phone> phone = new HashSet<Phone>();
        private StudentIdentifier studentIdentifier;
        private Set<Suspension> suspended = new HashSet<Suspension>();
        private Set<Suspension> instigater = new HashSet<Suspension>();
        private Set<Cohort> cohortCoordinating = new HashSet<Cohort>();
     
     
        @OneToMany(mappedBy = "personCoordinating")
        public Set<Cohort> getCohortCoordinating() {
            return cohortCoordinating;
        }
     
        public void setCohortCoordinating(Set<Cohort> cohortCoordinating) {
            this.cohortCoordinating = cohortCoordinating;
        }
     
        public void setCohort(Set<Cohort> cohort) {
            this.cohort = cohort;
        }
     
        @ManyToMany
        public Set<Cohort> getCohort() {
            return cohort;
        }
     
     
     
        public void setInstigater(Set<Suspension> instigaterPerson) {
            this.instigater = instigaterPerson;
        }
     
        public void setSuspended(Set<Suspension> suspendedPerson) {
            this.suspended = suspendedPerson;
        }
     
        @OneToMany(mappedBy = "instigaterPerson", cascade = {CascadeType.REMOVE})
        public Set<Suspension> getInstigater() {
            return instigater;
        }
     
        @ManyToMany(mappedBy = "suspendedPerson", cascade = {CascadeType.REMOVE})
        public Set<Suspension> getSuspended() {
            return suspended;
        }
     
        @OneToOne
        public StudentIdentifier getStudentIdentifier() {
            return studentIdentifier;
        }
     
        public void setStudentIdentifier(StudentIdentifier studentIdentifier) {
            this.studentIdentifier = studentIdentifier;
        }
     
        @ManyToMany(mappedBy = "personPhone", cascade = {CascadeType.REMOVE})
        public Set<Phone> getPhone() {
            return phone;
        }
     
        public void setPhone(Set<Phone> phone) {
            this.phone = phone;
        }
     
        @ManyToOne
        public PersonStatus getStatus() {
            return status;
        }
     
        public void setStatus(PersonStatus personStatus) {
            this.status = personStatus;
        }
     
        @ManyToOne
        public Organisation getOrganisation() {
            return organisation;
        }
     
        public void setOrganisation(Organisation organisation) {
            this.organisation = organisation;
        }
     
        @OneToMany(mappedBy = "personEmail", cascade = {CascadeType.REMOVE})
        public Set<Email> getEmail() {
            return email;
        }
     
        public void setEmail(Set<Email> email) {
            this.email = email;
        }
     
        public void setConfirmationDate(Timestamp confirmationDate) {
            this.confirmationDate = confirmationDate;
        }
     
        public void setCreationDate(Timestamp creationDate) {
            this.creationDate = creationDate;
        }
     
        public void setLastLogin(Timestamp lastLogin) {
            this.lastLogin = lastLogin;
        }
     
        public Timestamp getConfirmationDate() {
            return confirmationDate;
        }
     
        public Timestamp getCreationDate() {
            return creationDate;
        }
     
        public Timestamp getLastLogin() {
            return lastLogin;
        }
     
        @Override
        public void setForename(String forename) {
            this.forename = forename;
        }
     
        @Override
        public void setPassword(String password) {
            this.password = password;
        }
     
        @Override
        public void setSurname(String surname) {
            this.surname = surname;
        }
     
        @Override
        public void setUsername(String username) {
            this.username = username;
        }
     
        @Column
        @Override
        public String getForename() {
            return forename;
        }
     
        @Column
        @Override
        public String getPassword() {
            return password;
        }
     
        @Column
        @Override
        public String getSurname() {
            return surname;
        }
     
        @Column
        @Override
        public String getUsername() {
            return username;
        }
     
        @Override
        public void setId(Long id) {
            this.id = id;
        }
     
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Override
        public Long getId() {
            return id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Person)) {
                return false;
            }
            Person other = (Person) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "jim.sums.db.Person[id=" + id + "]";
        }
     
        @Override
        public boolean isUserInRole(String role) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    Organisation.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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package jim.sums.db;
     
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
     
    /**
     *
     * @author BreyA
     */
    @Entity
    public class Organisation implements Serializable {
     
        private static final long serialVersionUID = 1L;
        private Long id;
        private String name;
        private String postalAddress;
        private String postcode;
        private String activity;
        private Set<Person> personOrganisation = new HashSet<Person>();
     
        @OneToMany(mappedBy = "organisation", cascade = {CascadeType.REMOVE})
        public Set<Person> getPersonOrganisation() {
            return personOrganisation;
        }
     
        public void setPersonOrganisation(Set<Person> personOrganisation) {
            this.personOrganisation = personOrganisation;
        }
     
        public void setActivity(String activity) {
            this.activity = activity;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public void setPostalAddress(String postalAddress) {
            this.postalAddress = postalAddress;
        }
     
        public void setPostcode(String postcode) {
            this.postcode = postcode;
        }
     
        @Column
        public String getActivity() {
            return activity;
        }
     
        @Column
        public String getName() {
            return name;
        }
     
        @Column
        public String getPostalAddress() {
            return postalAddress;
        }
     
        @Column
        public String getPostcode() {
            return postcode;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public Long getId() {
            return id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Organisation)) {
                return false;
            }
            Organisation other = (Organisation) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "jim.sums.db.Organisation[id=" + id + "]";
        }
    }
    StudentIdentifier.java :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package jim.sums.db;
     
    import java.io.Serializable;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToOne;
     
    /**
     *
     * @author BreyA
     */
    @Entity
    public class StudentIdentifier implements Serializable {
        private static final long serialVersionUID = 1L;
        private Long id;
        private String hemis;
        private Person personStudent;
     
     
        @OneToOne(mappedBy="studentIdentifier", cascade={CascadeType.REMOVE})
        public Person getPersonStudent() {
            return personStudent;
        }
     
        public void setPersonStudent(Person person) {
            this.personStudent = person;
        }
     
     
        public void setHemis(String hemis) {
            this.hemis = hemis;
        }
     
        @Column
        public String getHemis() {
            return hemis;
        }
     
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public Long getId() {
            return id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof StudentIdentifier)) {
                return false;
            }
            StudentIdentifier other = (StudentIdentifier) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "jim.sums.db.StudentNumber[id=" + id + "]";
        }
     
    }
    La suppression en cascade fonctionne pour tous sauf pour "Organisation" et "StudentIdentifier".

    StudentIdentifier est de type @OneToOne
    Organisation est de type @ManyToOne (dans Person) et @OneToMany (dans Organisation)

    Donc quand je supprime une personne, son id d'Organisation ou de StudentIdentifier (c'est un ou l'autre) est supprimé et la pas de suppression en cascade.

    Voila, j'espere que vous avez compris mon probleme.

    Merci de m'aider

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    45
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 45
    Points : 35
    Points
    35
    Par défaut
    tu n'aurai pas oublier cascade = {CascadeType.REMOVE} dans tes annotation

  3. #3
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    167
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 167
    Points : 85
    Points
    85
    Par défaut
    ben nan comme tu peux le voir je les ai mises ces annotations dans les classes Organisation et StudentIdentifier

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    45
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 45
    Points : 35
    Points
    35
    Par défaut
    le remove que tu parle c'est si tu efface l'objet Organisation ou StudentIdentifier il effacera la personne concerné. il faut plutot le metre dans ta classe personne pour qu'il comprend que si tu efface la personne alors il effasera en cascade les autre objet
    sinon essai @org.hibernate.annotations.Cascade = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN}
    qui est un rajout a la librairie javax.peristence
    tu le met
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    @OneToOne
    @org.hibernate.annotations.Cascade = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN} 
    public StudentIdentifier getStudentIdentifier() {
            return studentIdentifier;
    }

  5. #5
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    167
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 167
    Points : 85
    Points
    85
    Par défaut
    Merci beaucoup j'ai finalement réussi à trouver une solution

    A bientôt !

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

Discussions similaires

  1. Probleme avec routeur en cascade
    Par FredericB dans le forum Hardware
    Réponses: 5
    Dernier message: 22/03/2013, 19h59
  2. Problème avec suppression en cascade
    Par agur29 dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 26/07/2007, 11h42
  3. Réponses: 1
    Dernier message: 19/07/2007, 14h53
  4. [ EJB ] [JBoss ] [ XDoclet ] probleme avec cascade-delete
    Par Houbbba dans le forum Wildfly/JBoss
    Réponses: 4
    Dernier message: 03/05/2006, 10h05
  5. [REDHAT] Problème de suppression avec dépendance
    Par byloute dans le forum RedHat / CentOS / Fedora
    Réponses: 1
    Dernier message: 21/04/2006, 18h19

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